Symfony custom filename for uploaded image files - image

I'm aware of VichUpload and namers, but I have to face two different file uploads, and I need special naming conventions that I'm unable to get with the current VichUpload documentation.
First file upload is for an entity called "School", which manages every single school, and its logo. So, taking as web root '../web/files/schools/', then I'd take the school_id and then the 'logo' folder with the uploaded file name, so it could be '../web/files/schools/000001/logo.png'.
Then, the second entity is 'students' to store the photo, with a school_id foreign key from School entity. So, the resulting file name would depend on the school id, and the student id, being the root for student '../web/files/schools/<school_id>/students/<student_id>.[jpg|png]', having student_id a six zero padding on the left.
This is the section in config.yml about this (updated info):
school_image:
upload_destination: "../web/files/images/schools"
uri_prefix: "/files/images/schools"
directory_namer:
service: vich_uploader.directory_namer_subdir
options: { property: 'schoolDirectory', chars_per_dir: 6, dirs: 1 }
namer:
service: vich_uploader.namer_property
options: { property: 'idSlug' }
inject_on_load: true
delete_on_update: true
delete_on_remove: true
student_image:
upload_destination: "../web/files/images/schools"
uri_prefix: "/files/images/schools"
directory_namer:
service: vich_uploader.directory_namer_subdir
options: { property: 'userDirectory', chars_per_dir: 6, dirs: 1 }
namer:
service: vich_uploader.namer_property
options: { property: 'userImage'}
inject_on_load: true
delete_on_update: true
delete_on_remove: true
in school entity (might work with a dirty workaround):
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="school_image", fileNameProperty="imageName")
*
* #Assert\Image()
*
* #var File
*/
private $imageFile;
public function getIdSlug()
{
$slug = sprintf("%06d", $this->getId());
return $slug;
}
public function getSchoolDirectory()
{
$slug = sprintf("%06d", $this->getId());
return $slug;
}
in student entity (not working, as explained below):
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* #Vich\UploadableField(mapping="student_image", fileNameProperty="imageName")
*
* #Assert\Image()
*
* #var File
*/
private $imageFile;
public function getUserDirectory()
{
$schoolDir = $this->getSchool()->getSchoolDirectory();
$dir = $schoolDir.'/students/'.sprintf("%06d", $this->getId());
return $dir;
}
public function getUserImage()
{
return $this->getUsername() . $this->getImageFile()->getExtension();
}
This setup with both "namer" and "directory_namer" seems to ignore the paths in directory_namer and use a path "<namer>/<namer>.ext" instead of "<directory_namer>/<namer>.ext". If I change the getUserImage() function and prepend the result of getUserDirectory() (i.e., the db stores "<directory_namer>/<namer>.ext" instead of just "<namer>.ext"), the "<directory_namer>" path is ignored and just "<namer>.ext" is created.
Since upload_prefix and uri_destination don't seem to handle variable data, how can I setup a namer or whatever to get this path for both cases?
BTW, I'm using Symfony 3.1 and composer hasn't allowed to update vich beyond "1.7.x-dev", according to bundled composer.json in vendor folder. If you find that it should work with this setup and the possible solution is to upgrade, I would thank to be pointed to the specific files which fix the problem, so I can manually paste whatever is wrong.
SOLUTION:
The problem was that, due to the "old" version, there was a missing directory_namer class, PropertyDirectoryNamer, with vich_uploader.namer_directory_property servicename, instead of vich_uploader.directory_namer_subdir, which was a wrong class for this purpose. I copied this class and registered it in the namer.xml file, and then I got the expected results. Now I'm going to try to mark this as solved.

You need to create custom directory namer class that implements
Vich\UploaderBundle\Naming\DirectoryNamerInterface
For example:
namespace App\Services;
use Vich\UploaderBundle\Mapping\PropertyMapping;
class SchoolDirectoryNamer implements DirectoryNamerInterface
{
public function directoryName($object, PropertyMapping $mapping): string
{
// do what you want with $object and $mapping
$dir = '';
return $dir;
}
}
Then, make it as a service.
services:
# ...
app.directory_namer.school:
class: App\Services\SchoolDirectoryNamer
Last step is config vich_uploader
vich_uploader:
# ...
mappings:
school:
upload_destination: school_image
directory_namer: app.directory_namer.school
student:
upload_destination: student_image
directory_namer: app.directory_namer.student
source : VichUploaderBundle - How To Create a Custom Directory Namer

Related

Error declaring MediaLibrary/CustomPathGenerator.php file

In laravel 9 with spatie/laravel-medialibrary 10 I tyry to make custom path for uploaded file
looking at docs : https://spatie.be/docs/laravel-medialibrary/v9/advanced-usage/using-a-custom-directory-structure
But making app/Services/MediaLibrary/CustomPathGenerator.php file :
<?php
namespace App\Services\MediaLibrary;
//namespace App\MediaLibrary;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\PathGenerator\PathGenerator;
//class CustomPathGenerator implements BasePathGenerator
class CustomPathGenerator implements PathGenerator
{
/*
* Get the path for the given media, relative to the root storage path.
*/
public function getPath(Media $media): string
{
return md5($media->id . config('app.key')) .'/';
}
/*
* Get the path for conversions of the given media, relative to the root storage path.
*/
public function getPathForConversions(Media $media): string
{
return md5($media->id . config('app.key')) .'/conversions/';
}
/*
* Get the path for responsive images of the given media, relative to the root storage path.
*/
public function getPathForResponsiveImages(Media $media): string {
return md5($media->id . config('app.key')) .'/responsive-images/';
}
}
I got error :
[2022-02-16 17:52:44] local.ERROR: Interface "App\Services\MediaLibrary\BasePathGenerator" not found {"userId":1,"exception":"[object] (Error(code: 0): Interface \"App\\Services\\MediaLibrary\\BasePathGenerator\" not found at /mnt/_work_sdb8/wwwroot/lar/hostels4j/app/Services/MediaLibrary/CustomPathGenerator.php:8)
Looks like header of my file is invalid.
Which is valid way to fix it ?
Thanks in advance!
You have defined the BasePathGenerator incorrectly please use the following instead
<?php
namespace App\Services\MediaLibrary;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator as BasePathGenerator;
class CustomPathGenerator implements BasePathGenerator {
/*
* Get the path for the given media, relative to the root storage path.
*/
public function getPath(Media $media): string {
return '';
}
/*
* Get the path for conversions of the given media, relative to the root storage path.
*/
public function getPathForConversions(Media $media): string {
return '';
}
/*
* Get the path for responsive images of the given media, relative to the root storage path.
*/
public function getPathForResponsiveImages(Media $media): string {
return '';
}
}
This should solve your issue.

Laravel tag all class implemeting an interface

I'm using Laravel 8 and i want to get all the classes that implements an Interface X.
I did it with symfony4 few month ago with DI :
services.yml
_instanceof:
App\Calculator\Budget\BudgetCalculatorInterface:
tags: ['app.budget_calculator']
App\Handler\CalculatorBudgetHandler:
arguments: [!tagged app.budget_calculator]
and then in my class CalculatorBudgetHandler.php
private $calculatorList = [];
public function __construct(iterable $calculatorList)
{
$this->calculatorList = $calculatorList;
}
public function __construct(iterable $calculatorList)
{
$this->calculatorList = $calculatorList;
}
public function calculate(array $data): float
{
foreach ($this->calculatorList as $calculator) {
if ($calculator->supports($data)) {
return $calculator->calculate($data);
}
}
}
but i dot not understand how to do it with Laravel. I think i have to pass all my classes in a bind or tag :
$this->app->tag([CpuReport::class, MemoryReport::class], 'reports');
thats mean if i got a new class implementing X, i have to add it in the bind/tag ?
I want to do it automatically .
thx !
I needed this too. Looked for a longer time and I basically found a solution. The bad thing about this is that in PHP classes aren't actually declared when you did not use them. So you'll have to either scan the entire project for classes and test each class to find classes implementing your interface or (better) you use the composer autoload class maps. There you could probably limit the searching scope for classes to a sub namespace.
A small but cool package working this way is this one: https://gitlab.com/hpierce1102/ClassFinder - basically it uses composer PSR4 classmaps and is in general fine performance wise.
Here is the solution to which I came:
// Add to service provider
private function tagByInterface(string $interfaceName, string $tagName, string $rootNamespace)
{
foreach (ClassFinder::getClassesInNamespace($rootNamespace, ClassFinder::RECURSIVE_MODE) as $className) {
$class = new \ReflectionClass($className);
if ($class->isAbstract() || $class->isInterface()) {
continue;
}
if ($class->implementsInterface($interfaceName)) {
$this->app->tag($className, $tagName);
}
}
}
Which can then be used like this in the register():
$this->tagByInterface(SomeInterface::class, 'some-tag', 'App\Domain\Something');
$this->app->when(SomeClass::class)->needs('$myServices')->giveTagged('some-tag');
As the classes are loaded using reflection, this operation still might take time if your root namespace is not properly set or too wide. Reflection is quick (as far as I know quicker than loading the information from cache), but you should still think of using a deferred provider for the task so that the search for implementing classes only triggers when it's actually needed.
Update some months later
This solution works, but might be a huge drain on performance if the project gets big. I'm now caching the tagged classes. Something like this:
use HaydenPierce\ClassFinder\ClassFinder as HPClassFinder;
use Illuminate\Contracts\Cache\Repository;
class InheritanceClassFinder
{
public function __construct(private ?Repository $cache = null)
{
}
public function findClassesImplementingOrExtending(string $interfaceOrClass, string $rootNamespace): array
{
if ($this->cache) {
return $this->cache->rememberForever(
'inheriting-classes-'.$interfaceOrClass,
fn () => $this->findClassesInheriting($interfaceOrClass, $rootNamespace));
}
return $this->findClassesInheriting($interfaceOrClass, $rootNamespace);
}
private function findClassesInheriting(string $interfaceOrClass, string $rootNamespace): array
{
$classes = [];
foreach (HPClassFinder::getClassesInNamespace($rootNamespace, HPClassFinder::RECURSIVE_MODE) as $className) {
if (!is_subclass_of($className, $interfaceOrClass)
|| ($class = new \ReflectionClass($className))->isAbstract() || $class->isInterface()) {
continue;
}
$classes[] = $className;
}
return $classes;
}
}
This means as long as the cache is injected, stuff will be loaded once and then taken from cache. I inject the cache only in production, so locally its a bit slower but always up to date. In production I throw away the cache with every deployment, so I get a fresh load once after every deployment.

Implementing file attachments in JHipster

I have a monolithic JHipster (v5.6.1) project, and I need to implement file attachments to an entity.
I don't know where to start with this. DTOs are sent to REST controllers to create or update them, how should I send the file?
I could attach the Base64 to my DTO and send it that way, but I'm not sure how (are there any angular plugins to accomplish this?). This would require extra work for the server and, I think, extra file size.
Another option is to send the entity (DTO) and file separately, again I'm not entirely sure how.
I see no reason to leave this question unanswered since I have implemented attachments successfully in several of my projects now.
If you prefer, you can skip this explanation and check the github repository I created at vicpermir/jhipster-ng-attachments. It's a working project ready to fire up and play.
The general idea is to have an Attachment entity with all the required fields (file size, name, content type, etc...) and set a many-to-many relation to any entity you want to implement file attachments for.
The JDL is something like this:
// Test entity, just to showcase attachments, this should
// be the entity you want to add attachments to
entity Report {
name String required
}
entity Attachment {
filename String required // Generated unique filename on the server
originalFilename String required // Original filename on the users computer
extension String required
sizeInBytes Integer required
sha256 String required // Can be useful for duplication and integrity checks
contentType String required
uploadDate Instant required
}
// ManyToMany instead of OneToMany because it will be easier and cleaner
// to integrate attachments into other entities in case we need to do it
relationship ManyToMany {
Report{attachments} to Attachment{reports}
}
I have both filename and originalFilename because one of my requirements was to keep whatever file name the user uploaded it with. The generated unique name that I use on the server side is transparent to the user.
Once you generate a project with a JDL like that, you will have to add the file payload to your DTO (or entity if you don't use DTOs) so that the server can receive it in base64 and store it.
I have this in my AttachmentDTO:
...
private Instant uploadDate;
// File payload (transient)
private byte[] file;
public Long getId() {
return id;
}
...
Then, you only have to process those byte arrays on the server side, store them and save a reference to the location into the database.
AttachmentService.java
/**
* Process file attachments
*/
public Set<AttachmentDTO> processAttachments(Set<AttachmentDTO> attachments) {
Set<AttachmentDTO> result = new HashSet<>();
if (attachments != null && attachments.size() > 0) {
for (AttachmentDTO a : attachments) {
if (a.getId() == null) {
Optional<AttachmentDTO> existingAttachment = this.findBySha256(a.getSha256());
if(existingAttachment.isPresent()) {
a.setId(existingAttachment.get().getId());
} else {
String fileExtension = FilenameUtils.getExtension(a.getOriginalFilename());
String fileName = UUID.randomUUID() + "." + fileExtension;
if (StringUtils.isBlank(a.getContentType())) {
a.setContentType("application/octet-stream");
}
Boolean saved = this.createBase64File(fileName, a.getFile());
if (saved) {
a.setFilename(fileName);
}
}
}
result.add(a);
}
}
return result;
}
What I do here is check if the attachment already exists (using the SHA256 hash). If it does I use that one, otherwise I store the new file and persist the new attachment data.
What's left now is to manage attachments on the client side. I created two components for this so it is extremely easy to add attachments to new entities.
attachment-download.component.ts
...
#Component({
selector: 'jhi-attachment-download',
template: , // Removed to reduce verbosity
providers: [JhiDataUtils]
})
export class JhiAttachmentDownloadComponent {
#Input()
attachments: IAttachment[] = [];
}
This just calls a mapping that takes the attachment ID, looks for the associated file on the server and returns that file for the browser to download. Use this component in your entity detail view with:
<jhi-attachment-download [attachments]="[your_entity].attachments"></jhi-attachment-download>
attachment-upload.component.ts
...
#Component({
selector: 'jhi-attachment-upload',
template: , // Removed to reduce verbosity
providers: [JhiDataUtils]
})
export class JhiAttachmentUploadComponent {
#Input()
attachments: IAttachment[] = [];
loadingFiles: number;
constructor(private dataUtils: JhiDataUtils) {
this.loadingFiles = 0;
}
addAttachment(e: any): void {
this.loadingFiles = 0;
if (e && e.target.files) {
this.loadingFiles = e.target.files.length;
for (let i = 0; i < this.loadingFiles; i++) {
const file = e.target.files[i];
const fileName = file.name;
const attachment: IAttachment = {
originalFilename: fileName,
contentType: file.type,
sizeInBytes: file.size,
extension: this.getExtension(fileName),
processing: true
};
this.attachments.push(attachment);
this.dataUtils.toBase64(file, (base64Data: any) => {
attachment.file = base64Data;
attachment.sha256 = hash
.sha256()
.update(base64Data)
.digest('hex');
attachment.processing = false;
this.loadingFiles--;
});
}
}
e.target.value = '';
}
getExtension(fileName: string): string {
return fileName.substring(fileName.lastIndexOf('.'));
}
}
Use this component in your entity update view with:
<jhi-attachment-upload [attachments]="editForm.get('attachments')!.value"></jhi-attachment-upload>
Once sent to the server, the files will be stored on the folder you configured in your application-*.yml separated in subdirectories by year and month. This is to avoid storing too many files on the same folder, which can be a big headache.
I'm sure many things could be done better, but this has worked for me.

October CMS extend System/Models/File

I trying to keep original file name when using System/Models/File, I got following code to extend this model:
namespace System\Models;
class NewFile extends File { public function fromPost($uploadedFile) { if ($uploadedFile === null) { return; }
$this->file_name = $uploadedFile->getClientOriginalName();
$this->file_size = $uploadedFile->getClientSize();
$this->content_type = $uploadedFile->getMimeType();
$this->disk_name = $this->getDiskName();
/*
* getRealPath() can be empty for some environments (IIS)
*/
$realPath = empty(trim($uploadedFile->getRealPath()))
? $uploadedFile->getPath() . DIRECTORY_SEPARATOR . $uploadedFile->getFileName()
: $uploadedFile->getRealPath();
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);
return $this;
It works with file itself, it keeps original name but problem is link to attached file is still being generated. Broke my mind but cant get this work. Can anyone elaborate how to fix it?
Oh I see it seems its try to use disk_name to generate URL
so you did well for saving an image
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);
but you just need to replace one line .. just undo your changes and make this one change
$this->file_name = $uploadedFile->getClientOriginalName();
$this->file_size = $uploadedFile->getClientSize();
$this->content_type = $uploadedFile->getMimeType();
// $this->disk_name = $this->getDiskName();
$this->disk_name = $this->file_name;
// use same file_name for disk ^ HERE
Link logic ( for referance only ) vendor\october\rain\src\Database\Attach\File.php and modules\system\models\File.php
/**
* Returns the public address to access the file.
*/
public function getPath()
{
return $this->getPublicPath() . $this->getPartitionDirectory() . $this->disk_name;
}
/**
* Define the public address for the storage path.
*/
public function getPublicPath()
{
$uploadsPath = Config::get('cms.storage.uploads.path', '/storage/app/uploads');
if ($this->isPublic()) {
$uploadsPath .= '/public';
}
else {
$uploadsPath .= '/protected';
}
return Url::asset($uploadsPath) . '/';
}
Just make disk_name also same as file_name so when file saved on disk it will use original name and when the link is generated it also use disk_name which is original file_name
now your link and file name are synced and will be same always.
if any doubt please comment.

What is the best way to limit a modules functionality by store or website

Basically we have a lot of clients that have multiple websites and we have to write a lot of modules for them. Sometimes an extension may override some functionality that Magento's core does by default and they want to do this for one store but not another. Obviously we can put logic in the code to see what store it is but I am thinking there is something more elegant way to do this.
Here is a common idiom I have used in the past. It works well when you are modifying the existing logic by overriding classes and modifying the functions on a one-off basis:
public function overriddenFunc($arg) {
if(!$this->checkIfModuleIsEnabledForStore()) {
return parent::overriddenFunc($arg);
}
// do your magic here
return $something;
}
It basically functions as a passthrough on the functionality override whenever the module is not enabled. Then, you can use a store-level configuration setting to turn the functionality on and off by store.
To keep yourself sane, make sure to override only the minimum necessary functions to get by.
Hope that helps!
Thanks,
Joe
Good question.
I would have solved this problem on another way.
Module structure:
Custom
| - Module
| - - Model
| - - - Product.php
| - - - Customer.php
For my opinion it is need to be created class that depends from store.
If you want to create some functionality for Store UK, you need to declare this class for UK store, write it in configuration file and call it with factory class.
For example in the config.xml
<config>
<stores>
<store_uk>
<catalog_product>Custom_Module_Model_Store_Uk_Product</product_attribute>
<customer>Custom_Module_Model_Store_Uk_Customer</customer>
</store_uk>
<store_en>
<catalog_product>Custom_Module_Model_Store_En_Product</catalog_product>
</store_en>
</stores>
</config>
Create class store router:
class Custom_Module_Model_Store_Router
{
public function callMethod($method, $args)
{
if (strpos($method, '/') !== false) {
$method = explode('/', $method);
}
if (count($method) != 2) {
return false;
}
$handler = $method[0];
$method = $method[1];
$object = $this->_getObject($handler);
if ($object) {
//already checked if method exists
retun $object->$method($args);
}
return false;
}
public function hasStoreMethod($method)
{
if (strpos($method, '/') !== false) {
$method = explode('/', $method);
}
if (count($method) != 2) {
return false;
}
$handler = $method[0];
$method = $method[1];
$object = $this->_getObject($handler);
if (method_exists($object, $method)) {
//Bingo
return true;
}
return false;
}
protected function _getObject($handler)
{
$storeCode = Mage::app()->getStore(true)->getCode();
$handlerClassName = Mage::getStoreConfig($storeCode . '/' . $handler);
if (empty($handlerClassName)) {
return false;
}
$handlerInstance = Mage::getModel($handlerClassName);
//here we can save instance into the _handlers etc.
return $handlerInstance;
}
}
This class will be as default customization
//in your custom module product class
Custom_Module_Model_Product extends Mage_Catalog_Model_Product_Attribute
{
public function getAttributes($groupId = null, $skipSuper = false)
{
$routerStore = Mage::getSingleton('custom_module/store_router');
if ($routerStore->hasStoreMethod('catalog_product/getAttributes')) {
$attributes = $routerStore->callMethod('catalog_product/getAttributes', array('groupId' => $groupId, 'skipSuper' => $skipSuper));
return $attributes;
}
return parent::getAttributes($groupId, $skipSuper);
}
}
And this class is store Uk class only
//custom module product class for uk store
Custom_Module_Model_Store_Uk_Product extends Mage_Catalog_Model_Product_Attribute
{
public function getAttributes($groupId = null, $skipSuper = false)
{
$attributes = parent::getAttributes($groupId, $skipSuper);
// do some specific stuff
return $attributes;
}
}
After this steps you will have clear customization classes with module structure listed below:
Custom
| - Module
| - - Model
| - - - Store
| - - - - Uk
| - - - - - Product.php
| - - - - - Customer.php
| - - - - En
| - - - - - Product.php
| - - - - Router.php
| - - - Product.php
| - - - Customer.php
I hope this will help for your multistore development
I think the only way to achieve this is to customize the logic how module is loaded it's configuration, because all rewrites depends on customization only.
My first idea how to do this was to override Mage_Core_Model_Config::_loadDeclaredModules() or Mage_Core_Model_Config::_getDeclaredModuleFiles() and check there store id before load config file, but I've realized, that store id isn't initialized yet when this methods is called: if you look at Mage_Core_Model_App::run() you will see that _initCurrentStore() is called later.
The second idea: customize fabric method Mage::getModel(). If you look at Mage_Core_Model_Config::getGroupedClassName() you'll see that it takes modules, blocks, helpers, etc configuration from node global. You could override this method to make it take all this configuration from node 'stores/current_store_code', so all rewrites will be loaded for current store only.
But I'm not sure on 100% are these solution implementable.

Resources