-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access to the original file in events #21
Comments
Hmm. Does not sound like a good idea to me. The time the cf.: https://gist.github.com/sheeep/5833126 But I've seen now that the original object will be kept in the request object, so this works as expected: var_dump($request->files->get('meep')->getClientOriginalName());
If you are using the chunked upload controller, a Nonetheless this is a problem. A possible solution would be to introduce a May I ask: What uploader frontend are you using? Some of them pass the filename and other properties in the request. And if you're using chunked uploads: Is the last chunk accessible? |
Thank you for your answer! I am using blueimp as frontend, and you were right, the last chunk as object is accessible, so the filename I can tell. However telling other information, such as mime-type depends on what storage are you using. If it is Gaufrette and an adapter that does not support metadata, you have no chance to tell the mime-type of the file then, except if you upload it, and stream it back immediately. However, if I do $file = $request->files->all()["files"][0];
var_dump($file->getSize()); on a not-chunked file in postupload/postpersist I can access it. The reason is, I am using Gaufrette, so the file is not moved, as it would be in your example, rather copied over. If it is chunked, then the file coming from the request is the last chunk, which does not exist by then so it throws a FileNotFoundException, as it should. Also blueimp offers you the option to upload multiple files in one request as documented here: At least I like the idea of a preUpload, it would definitely add more flexibility than just passing another thing to the current events. |
Is this the mime type sent by the browser? If so, I'd rather not rely on information sent from a client. Take a look at the AllowedMimeTypeValidationListener. You can determine the mime type by using the built in FileInfo extension. (edit: Given that the temporary file still exists)
This makes perfectly sense. But I think an implementation should behave the same (as far as it is possible) if you're using Gaufrette or the native filesystem storage.
You're absolutely right! It seems I've implemented this unintentionally. :)
Good to know. I'll implement it as soon as possible. Thanks for your detailed report! |
Just to clarify, the mime type is not for validation, it is to tell if I should create a thumbnail or not, and to later on properly set the content type when the file is accessed. Thanks for the quick reply. |
This event will be given an instance of UploadedFile instead of a moved file. Addresses #21.
Let me know if this works for you. :) I also added a test to assure that it is possible to pass variables through the request object. (If you want to know how..) |
Yes it does, also solves my problem. Thank you! |
Could you trim "0_" from the beginning of the reassembled file (it would be fine only in the object too)? I can access the correct name from the request, but it would be cleaner that way. |
I don't think this is something we should have added to this bundle because this is part of your personal business-logic. Is the |
The first chunk is called 0_filename and the rest are assembled to that, so the final file will be the same name. I noticed your comment in the above function, try:
Accessing the filename in the request gives the original, so blueimp does not append/prepend anything to it. |
Ah I see. My bad! :) I'll have a look as soon as possible. |
Happens to the best of us ;) Thank you, yet again. |
Removed a rather hacky way of determine which of the chunks to use as a base, and replaced it with an Iterator as mentioned in #21.
Until now, the reassembled file shared the name with the very first chunk uploaded. It had the form {prefix}_{filename}. This commit fixes this issue and addresses #21 with it.
|
Yes, it gives back the correct name. Also looks better ;) |
The renaming broke pretty critically after 9dbd905. In the following for better understanding: The base gets renamed from Here comes the real problem: The last request should (I am assuming here) always be the last, as it is named by But the critical part is, from a file where we assume the order of chunks is 1-2-3-4, we end up getting a file where the order is 3-1-2-4. Md5sum definitely differs. A solution could be to only call the rename with the last chunk - in the mean time users should be warned that the files uploaded in the meantime are probably broken. |
This is pretty critical. Let's see if I can give it a shot.
I guess the line which causes all these troubles is the following $assembled = $assembled->move($base->getPath(), preg_replace('/^(.+?)_/', '', $base->getBasename())); If I get you right, the file should not be renamed until it is the last chunk to assemble. Is this correct? If so, the best solution would be to add a parameter |
I agree, that was my conclusion too. Happy to see your quick reaction. Just to be sure you could also switch the regexp to match only numbers, if something breaks again that will lower the "casualties". |
Otherwise you will end up having a completly broken order of reassembling. Addresses: #21
Renaming of chunks will now only happen if it is the last chunk. @mitom Can you verify this?
Changed. Good point. |
The md5sum of the original file and the uploaded file match now. But I don't get this what if the load-distribution is off and the files need to be reassembled in the end? |
I was a bit jazzed ;) Readded shortly after the push in 52865bd. |
Then I believe this is once again fixed. For the sakes of those who have un-replaceable files corrupted, it would be worth a try to reorder them, however it is really dependant on the original filename and size. |
Hmm. I can't think of any solution for reordering that would work in any case. |
Hi, I'm testing the bundle with Symfony 2.1.6 and the last bundle tag version 0.9.7 with bluimp as frontend, but I have I problem with chunks upload. |
Hey,
Could you also pass the original file to the events?
The reason is that the namer gets called before the events, so what we get is already a renamed file, so we don't know the original name. For me it would be important to know the exact filename (case-sensitive) and store it in the db with other properties, but rename the files to a reasonable one.
If the file is not chunked and there is only 1 in the request, this can be done by accessing it directly in the request, however it won't work for chunked files, or if there are more files/request.
It would be possible to manage the database thing in the namer too, but I'd need to fetch the entity again in postPersist to generate a proper response.
Let me know your opinion, maybe there is a better way. Thanks.
The text was updated successfully, but these errors were encountered: