Skip to content

Commit

Permalink
Implement functionality to hide filename during upload
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyCupic committed Oct 15, 2023
1 parent ae769f3 commit 2b1b825
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions bundle/Controller/Resource/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function __invoke(Request $request): Response
null,
[],
$uploadContext,
$request->request->getBoolean('hide_filename'),
);

try {
Expand Down
2 changes: 1 addition & 1 deletion bundle/Resources/public/css/remotemedia.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion bundle/Resources/public/js/remotemedia.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion bundle/Resources/views/app/interactions.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
{% set folder = folder|default(null) %}
{% set upload_context = upload_context|default([]) %}
{% set disable_upload = disable_upload|default(false) %}
{% set hide_filename = hide_filename|default(false) %}

{% set location = null %}
{% if location_id %}
Expand Down Expand Up @@ -123,7 +124,8 @@
'parentFolder': parent_folder,
'folder': folder,
'uploadContext': upload_context,
'disableUpload': disable_upload
'disableUpload': disable_upload,
'hideFilename': hide_filename,
} %}

{% set browse_url = '' %}
Expand Down
3 changes: 2 additions & 1 deletion bundle/Resources/views/form/remote_media_type.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'parent_folder': form.vars.parent_folder,
'folder': form.vars.folder,
'upload_context': form.vars.upload_context|default([]),
'disable_upload': form.vars.disable_upload|default(false)
'disable_upload': form.vars.disable_upload|default(false),
'hide_filename': form.vars.hide_filename|default(false)
} %}
{% endblock %}
8 changes: 8 additions & 0 deletions docs/FORM.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ Every location has a string where you can put a descriptive info where this loca

This parameter enables you to override the source text.

#### `disable_upload`

This parameter enables you to disable the upload, which means that users will be able only to select one of the existing resources.

#### `hide_filename`

This parameter enables filename hiding; instead of original filename, it will use file's MD5 hash as a public ID. Original filename will be stored in the context though and can be used, if needed.

### Example

Let's say that you have a form for digital products in a webshop and you want to limit editors to be able to upload only protected files inside a specific folder. You want to also add some context.
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/UploadModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export default {
data.append('folder', this.selectedFolder);
data.append('overwrite', this.overwrite);
data.append('visibility', this.visibility);
data.append('hide_filename', this.config.hideFilename);
for (const [key, value] of Object.entries(this.config.uploadContext)) {
data.append(`upload_context[${key}]`, value);
Expand Down
1 change: 1 addition & 0 deletions frontend/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const initInteractionsVue = (el) => {
folder: null,
uploadContext: {},
disableUpload: false,
hideFilename: false,
},
selectedImage: {
id: '',
Expand Down
8 changes: 7 additions & 1 deletion lib/API/Upload/ResourceStruct.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct(
private ?string $altText = null,
private ?string $caption = null,
private array $tags = [],
private array $context = []
private array $context = [],
private bool $hideFilename = false,
) {}

public function getFileStruct(): FileStruct
Expand Down Expand Up @@ -92,4 +93,9 @@ public function getContext(): array
{
return $this->context;
}

public function doHideFilename(): bool
{
return $this->hideFilename;
}
}
5 changes: 5 additions & 0 deletions lib/Core/Provider/Cloudinary/Resolver/UploadOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use function explode;
use function in_array;
use function is_string;
use function md5_file;
use function preg_replace;
use function rtrim;

Expand All @@ -41,6 +42,10 @@ public function resolve(ResourceStruct $resourceStruct): array

$publicId = $this->appendExtension($fileName, $resourceStruct->getFileStruct());

if ($resourceStruct->doHideFilename()) {
$publicId = md5_file($resourceStruct->getFileStruct()->getUri());
}

if ($resourceStruct->getFolder()) {
$publicId = $resourceStruct->getFolder()->getPath() . '/' . $publicId;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Form/Type/RemoteMediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function configureOptions(OptionsResolver $resolver): void
'upload_context' => [],
'location_source' => null,
'disable_upload' => false,
'hide_filename' => false,
]);
}

Expand Down Expand Up @@ -111,6 +112,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
: null,
'upload_context' => $uploadContext,
'disable_upload' => $options['disable_upload'] ?? false,
'hide_filename' => $options['hide_filename'] ?? false,
]);
}
}

0 comments on commit 2b1b825

Please sign in to comment.