Retrieve file from storage and cast it to UploadedFile
Hi all,
My question is fairly simple and for some reason I can't figure it out.
I have files stored in the storage folder in my app and sometimes, I want to be able to retrieve one of these existing files and use it has a UploadedFile object in my code.
I manage to get the raw string contents of the file using the Storage::get method but how can I initialize a new UploadedFile object from that string ? Basically, I want to be able to play with the file the same way I would do with a file coming from a form request.
I need to extract some text from a stored file on update and I want my function to return a UploadedFile object mostly for code reuse because the extraction method is the same on create and on update.
I do this for maintenance purposes, so this way I can always be sure that I am working with a UploadedFile object through out the rest of the code. But you are right I could have done without it and maybe I will.
mostly for code reuse because the extraction method is the same on create and on update.
@chess So why dont you just extract this code into another class, and make a method taking a file as an argument to do this process (a file, not an uploaded file, this is more flexible, because an uploaded file is just an extended file). Then you can use this class anywhere you need to process a file, wherever it comes from.
A controller method (I guess it is what you want to reuse) is not made for code reuse / being called as a regular function, it is made for handling a request. But what's inside a controller can be reused anywhere if coded properly.
" So why dont you just extract this code into another class, and make a method taking a file as an argument to do this process (a file, not an uploaded file, this is more flexible, because an uploaded file is just an extended file). Then you can use this class anywhere you need to process a file, wherever it comes from. "
Exactly what I did, this code was not for a controller method.