Allow to upload files to attachments by passing a base64 encoded file

This commit is contained in:
Jan Böhmer 2024-03-03 18:40:35 +01:00
parent 0824e11570
commit 0c33059c4e
6 changed files with 261 additions and 32 deletions

View file

@ -35,6 +35,8 @@ use ApiPlatform\Metadata\Post;
use App\ApiPlatform\DocumentedAPIProperty;
use App\ApiPlatform\Filter\EntityFilter;
use App\ApiPlatform\Filter\LikeFilter;
use App\ApiPlatform\HandleAttachmentsUploadsProcessor;
use App\EntityListeners\AttachmentUploadListener;
use App\Repository\AttachmentRepository;
use App\EntityListeners\AttachmentDeleteListener;
use Doctrine\DBAL\Types\Types;
@ -68,12 +70,13 @@ use LogicException;
operations: [
new Get(security: 'is_granted("read", object)'),
new GetCollection(security: 'is_granted("@attachments.list_attachments")'),
new Post(securityPostDenormalize: 'is_granted("create", object)'),
new Post(securityPostDenormalize: 'is_granted("create", object)', ),
new Patch(security: 'is_granted("edit", object)'),
new Delete(security: 'is_granted("delete", object)'),
],
normalizationContext: ['groups' => ['attachment:read', 'attachment:read:standalone', 'api:basic:read'], 'openapi_definition_name' => 'Read'],
denormalizationContext: ['groups' => ['attachment:write', 'attachment:write:standalone', 'api:basic:write'], 'openapi_definition_name' => 'Write'],
processor: HandleAttachmentsUploadsProcessor::class,
)]
#[DocumentedAPIProperty(schemaName: 'Attachment-Read', property: 'media_url', type: 'string', nullable: true,
description: 'The URL to the file, where the attachment file can be downloaded. This can be an internal or external URL.',
@ -132,6 +135,14 @@ abstract class Attachment extends AbstractNamedDBElement
*/
protected const ALLOWED_ELEMENT_CLASS = AttachmentContainingDBElement::class;
/**
* @var AttachmentUpload|null The options used for uploading a file to this attachment or modify it.
* This value is not persisted in the database, but is just used to pass options to the upload manager.
* If it is null, no upload process is started.
*/
#[Groups(['attachment:write'])]
protected ?AttachmentUpload $upload = null;
/**
* @var string|null the original filename the file had, when the user uploaded it
*/
@ -192,6 +203,31 @@ abstract class Attachment extends AbstractNamedDBElement
}
}
/**
* Gets the upload currently associated with this attachment.
* This is only temporary and not persisted directly in the database.
* @internal This function should only be used by the Attachment Submit handler service
* @return AttachmentUpload|null
*/
public function getUpload(): ?AttachmentUpload
{
return $this->upload;
}
/**
* Sets the current upload for this attachment.
* It will be processed as the attachment is persisted/flushed.
* @param AttachmentUpload|null $upload
* @return $this
*/
public function setUpload(?AttachmentUpload $upload): Attachment
{
$this->upload = $upload;
return $this;
}
/***********************************************************
* Various function
***********************************************************/

View file

@ -0,0 +1,57 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Entity\Attachments;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Serializer\Attribute\Groups;
/**
* This is a DTO representing a file upload for an attachment and which is used to pass data to the Attachment
* submit handler service.
*/
class AttachmentUpload
{
public function __construct(
/** @var UploadedFile|null The file which was uploaded, or null if the file should not be changed */
public readonly ?UploadedFile $file,
/** @var string|null The base64 encoded data of the file which should be uploaded. */
#[Groups(['attachment:write'])]
public readonly ?string $data = null,
/** @vaar string|null The original filename of the file passed in data. */
#[Groups(['attachment:write'])]
public readonly ?string $filename = null,
/** @var bool True, if the URL in the attachment should be downloaded by Part-DB */
#[Groups(['attachment:write'])]
public readonly bool $downloadUrl = false,
/** @var bool If true the file will be moved to private attachment storage,
* if false it will be moved to public attachment storage. On null file is not moved
*/
#[Groups(['attachment:write'])]
public readonly ?bool $private = null,
/** @var bool If true and no preview image was set yet, the new uploaded file will become the preview image */
#[Groups(['attachment:write'])]
public readonly ?bool $becomePreviewIfEmpty = true,
) {
}
}