Saturday 22 December 2018

How to save uploaded files on a disk

I wrote a very simple file uploader, which calculates hash of the file content, adds it to the file name (so that files with the same name do not get overwritten, but if the same file is re-uploaded it overwrites the previously uploaded file), and saves the file in the root of the specified directory.There are a couple of problems with my super-naive approach.First, all files end up in a single directory. Presumably, as the number of files grows, this can slow down the time to access files. I googled for a good solution, but found only this article on Medium discussing how to build a directory structure out of the hash of file name. Is this a standard approach? Do you know of any pre-existing packages that already do something like that?The other problem is that calculating the hash of file content takes a long time if the file is large, and this CPU-intensive operation blocks the main thread. What is a good way to offload this operation from the main thread? (Sorry for asking such a noob question.)

Submitted December 22, 2018 at 10:08PM by azangru

No comments:

Post a Comment