Skip to content

Commit

Permalink
PS-732 databox - add feat delete AssetFileVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
4rthem committed Nov 25, 2024
1 parent e4d6e47 commit 891ae57
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Alchemy\MessengerBundle\Attribute\MessengerMessage;

#[MessengerMessage('p2')]
final readonly class FileDelete
final readonly class DeleteFileFromStorage
{
public function __construct(
private array $paths,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
readonly class FileDeleteHandler
readonly class DeleteFileFromStorageHandler
{
public function __construct(
private FileStorageManager $storageManager,
) {
}

public function __invoke(FileDelete $message): void
public function __invoke(DeleteFileFromStorage $message): void
{
foreach ($message->getPaths() as $path) {
$this->storageManager->delete($path);
Expand Down
19 changes: 19 additions & 0 deletions databox/api/src/Consumer/Handler/File/DeleteFilesIfOrphan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Consumer\Handler\File;

use Alchemy\MessengerBundle\Attribute\MessengerMessage;

#[MessengerMessage('p2')]
final readonly class DeleteFilesIfOrphan
{
public function __construct(
private array $ids,
) {
}

public function getIds(): array
{
return $this->ids;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace App\Consumer\Handler\File;

use App\Entity\Core\File;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use Symfony\Component\Messenger\MessageBusInterface;

#[AsMessageHandler]
readonly class DeleteFilesIfOrphanHandler
{
public function __construct(
private EntityManagerInterface $em,
private MessageBusInterface $bus,
) {
}

public function __invoke(DeleteFilesIfOrphan $message): void
{
$repo = $this->em->getRepository(File::class);
foreach ($message->getIds() as $id) {
$this->em->wrapInTransaction(function () use ($id, $repo): void {
$file = $repo->find($id);
if ($file instanceof File) {
$path = null;
if (File::STORAGE_S3_MAIN === $file->getStorage()) {
$path = $file->getPath();
}

$this->em->remove($file);
$this->em->flush();

if (null !== $path) {
$this->bus->dispatch(new DeleteFileFromStorage([$path]));
}
}
});
}
}
}
27 changes: 22 additions & 5 deletions databox/api/src/Doctrine/Listener/FileListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
namespace App\Doctrine\Listener;

use Alchemy\MessengerBundle\Listener\PostFlushStack;
use App\Consumer\Handler\File\FileDelete;
use App\Consumer\Handler\File\DeleteFileFromStorage;
use App\Consumer\Handler\File\DeleteFilesIfOrphan;
use App\Entity\Core\Asset;
use App\Entity\Core\AssetFileVersion;
use App\Entity\Core\AssetRendition;
use App\Entity\Core\File;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\Common\EventSubscriber;
Expand All @@ -22,14 +26,27 @@ public function __construct(private PostFlushStack $postFlushStack)
public function preRemove(PreRemoveEventArgs $args): void
{
$object = $args->getObject();
$em = $args->getObjectManager();

if ($object instanceof File) {
if (File::STORAGE_S3_MAIN === $object->getStorage()) {
$this->postFlushStack->addBusMessage(new FileDelete([$object->getPath()]));
}
if ($object instanceof Asset) {
$this->addFileToDelete($object->getSource());
} elseif ($object instanceof AssetRendition) {
$this->addFileToDelete($object->getFile());
} elseif ($object instanceof AssetFileVersion) {
$this->addFileToDelete($object->getFile());
}
}

private function addFileToDelete(?File $file): void
{
if (!$file) {
return;
}

$this->postFlushStack->addBusMessage(new DeleteFilesIfOrphan([$file->getId()]));
}


public function getSubscribedEvents(): array
{
return [
Expand Down
1 change: 0 additions & 1 deletion databox/api/src/Entity/Core/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#[ApiResource(
shortName: 'file',
operations: [

],
normalizationContext: [
'groups' => [File::GROUP_LIST],
Expand Down
8 changes: 7 additions & 1 deletion databox/client/src/api/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,12 @@ export async function getAssetAttributes(
return res.data['hydra:member'];
}

const assetFileVersionEntity = 'asset-file-versions';

export async function getAssetFileVersions(
assetId: string | string[]
): Promise<ApiCollectionResponse<AssetFileVersion>> {
const res = await apiClient.get(`/asset-file-versions`, {
const res = await apiClient.get(`/${assetFileVersionEntity}`, {
params: {
assetId,
},
Expand All @@ -168,6 +170,10 @@ export async function getAssetFileVersions(
return getHydraCollection(res.data);
}

export async function deleteAssetFileVersion(id: string): Promise<void> {
await apiClient.delete(`${assetFileVersionEntity}/${id}`);
}

export enum AttributeBatchActionEnum {
Set = 'set',
Replace = 'replace',
Expand Down
12 changes: 12 additions & 0 deletions databox/client/src/components/Dialog/Asset/AssetFileVersion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {useTranslation} from 'react-i18next';
import DownloadIcon from '@mui/icons-material/Download';
import SaveAsButton from '../../Media/Asset/Actions/SaveAsButton';
import DateTime from '../../Ui/DateTime';
import DeleteIcon from "@mui/icons-material/Delete";

const cardProps = {
elevation: 2,
Expand All @@ -33,12 +34,14 @@ type Props = {
asset: Asset;
version: AssetFileVersion;
dimensions: Dimensions;
onDelete?: () => void;
};

export function AssetFileVersionCard({
version: {file, name, createdAt},
asset,
dimensions,
onDelete,
}: Props) {
const {t} = useTranslation();

Expand Down Expand Up @@ -93,6 +96,15 @@ export function AssetFileVersionCard({
/>
</>
)}
{onDelete ? (
<Button
onClick={() => onDelete()}
color={'error'}
startIcon={<DeleteIcon />}
>
{t('asset_version.delete', 'Delete Version')}
</Button>
) : null}
</>
}
/>
Expand Down
28 changes: 27 additions & 1 deletion databox/client/src/components/Dialog/Asset/AssetFileVersions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import {useEffect, useState} from 'react';
import {Asset, AssetFileVersion} from '../../../types';
import {DialogTabProps} from '../Tabbed/TabbedDialog';
import ContentTab from '../Tabbed/ContentTab';
import {getAssetFileVersions} from '../../../api/asset';
import {deleteAssetFileVersion, getAssetFileVersions} from '../../../api/asset';
import {
AssetFileVersionCard,
AssetFileVersionSkeleton,
} from './AssetFileVersion';
import {useTranslation} from 'react-i18next';
import ConfirmDialog from "../../Ui/ConfirmDialog.tsx";
import {toast} from "react-toastify";
import {useModals} from "@alchemy/navigation";

type Props = {
data: Asset;
Expand All @@ -21,11 +24,33 @@ const maxDimensions = {
export default function AssetFileVersions({data, onClose, minHeight}: Props) {
const {t} = useTranslation();
const [versions, setVersions] = useState<AssetFileVersion[]>();
const {openModal} = useModals();

useEffect(() => {
getAssetFileVersions(data.id).then(d => setVersions(d.result));
}, []);


const onDelete = async (id: string) => {
openModal(ConfirmDialog, {
title: t(
'asset_version_delete.confirm',
'Are you sure you want to delete this version?'
),
onConfirm: async () => {
await deleteAssetFileVersion(id);

setVersions(versions?.filter(v => v.id !== id));
toast.success(
t(
'asset_version_delete.confirmed',
'Version has been deleted!'
) as string
);
},
});
}

return (
<ContentTab
onClose={onClose}
Expand Down Expand Up @@ -53,6 +78,7 @@ export default function AssetFileVersions({data, onClose, minHeight}: Props) {
asset={data}
version={v}
dimensions={maxDimensions}
onDelete={() => onDelete(v.id)}
/>
);
})}
Expand Down

0 comments on commit 891ae57

Please sign in to comment.