Closed jee7 closed 2 years ago
Ok. It's doable by implementing a callback that will do the str_replace on the path and save the result to another field in the entity. Still, seems it would be a nice feature to have already made and available via an annotation.
@jee7 can you explain how you did this? For strangest of reasons I am using fileName to create a link to the file, and it used to work with appendNumber, but now I cannot get to latest files as it always resolves to the original file uploaded.
My problem was that I did not want a full path storage/files/submission/file.zip
to be stored in the DB. Because it depends on the environment (storage/files
) and the entity (submission
). So I implemented a afterUpload
callback, which removed the first part, leaving only the unique file name. Something like this:
public function afterUpload($info) {
$this->changedFileName = str_replace($this->getPath() .'/', '', $info['filePath']);
$this->fileType = $info['fileMimeType'];
$this->fileSize = $info['fileSize'];
}
public function getPath() {
return $this->getBasePath() . $this->getRelativePath();
}
public static function getBasePath() {
return storage_path(self::getRelativeBasePath());
}
public static function getRelativeBasePath() {
return 'files/submissions/';
}
In you Uploadable annotation you add callback="afterUpload"
.
The getRelativePath()
actually returns another app specific folder structure inside the submission
folder (based on owning entity ID-s).
I did the same thing but with different function name - changed that so it's easier to talk:
public function afterUpload(array $fileInformation) {
//$this->fileName = $fileInformation['fileName'];
dump($fileInformation);
dump(array_pop(explode('/',$fileInformation['filePath'])));
$this->setFileName(array_pop(explode('/',$fileInformation['filePath'])));
}
Dumps return what I want - the fileName with appended number but my problme now is how do I save such entity (from itself?) ? The new fileName information is not persisted of course since it happens AFTER uploading a file.
Oh, I forgot to mention. In the annotation there is also pathMethod="getPath"
.
But, yes, it is called after upload, but it should be before persist. Are you sure it is not?
@jee7 yes I'm sure. When the code above is invoked the file name does not change. I don't use file entity outside of Document entity I'm using so all data and a file is written into it but that should not be the problem here.
How can I get the fileInfo object from the entity? I might be able to use the "PreUpdate" callback if it's available at that time.
Interesting. It changes for me. I added some callbacks and:
string 'Pre Persist: ' (length=13)
string 'After Upload: ddr2-11.jpg' (length=25)
string 'Post Persist: ddr2-11.jpg' (length=25)
The number 11 there is the appended number.
Although I am creating a new entity each time (because I need the previous files to also be available). I did not test with updating the entity, but it should work in the same order.
Also, I'm using:
"name": "laravel-doctrine/extensions",
"version": "1.0.2",
"name": "laravel-doctrine/orm",
"version": "1.0.10",
"name": "doctrine/orm",
"version": "v2.5.1",
So I might be a little outdated. Maybe this logic has changed...
I'm not using Laravel types but Symfony ones, so there may be something different between them, also I found that in production it seems to work.. most of the times as expected but not on dev. Either it was updated and stopped working or there are other aspects that I don't know of.
This is still an issue. The fix is simple. I will try to setup a PR
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
IIUC this should have been fixed in https://github.com/doctrine-extensions/DoctrineExtensions/pull/2460
When I'm using the appendNumber=true, then the number is added to the filePath, but not the fileName. As I understand, there might be some people who don't want the number to be added to the fileName (as the number is only to distinguish the files on the server, and the end-user shouldn't be aware of it). I on the other hand would want the number to be available in my entity without having to store the filePath in the database. The idea is that the filePath seems to be an absolute path and I don't want this stored. What if the absolute path of my application changes, then I would need to update every entry in my database. I want to store only the fileName (or a relativePath, but there doesn't seem to be such a thing here). The fileName should represent the file name that is located on disk. Although, there can be two annotations, one that specifies the file name on disk and the other that specifies the original file name.