grails file upload - file

Hey. I need to upload some files (images/pdf/pp) to my SQLS Database and thereafter, download it again. I'm not sure what is the best solution - store it as bytes, or store it as file (not sure if possible). I need later to databind multiple domain classes together with that file upload.
Any help would be very much apreciated,
JM

saving files in the file system or in the DB is a general question which is asked here several times.
check this: Store images(jpg,gif,png) in filesystem or DB?
I recommend to save the files in the file system and just save the path in the DB.
(if you want to work with google app-engine though you have to save the file as byte array in the DB as saving files in the file system is not possible with google app-engine)
To upload file with grails check this: http://www.grails.org/Controllers+-+File+Uploads

Related

How can I deal with the file which users upload?

If I have a website, and every user can upload his or her file, after I get this file, how can I deal with this file.
Should I store it to a specific folder(like upload), and store the file path in a specific database(for example table upload_file)?
Or should I just store this file in the database, if I can, how can I apply this to reality??
I think the first way is much better, but can I store all upload files in the same folder, or should I make a folder for each user?
Maybe I can encrypt the file name with md5 or anothers?
I do not know in real website development, how can you deal with the file, and keep the relationship with the user?
Thanks for your help !!
If they are small, just store it in the database;
Otherwise store they in a floder

Which Drupal 7 database table is file uploaded to?

I am working on drupal 7 which has content type that has a filed "file" and the file is uploaded to mysql database.
I would like to find the table where the file is stored as binary BLOB type.
Know the node ID.
Unless you have something special installed that I'm not familiar with, the files themselves aren't stored as binaries/serialized/etc in the DB. The file table (or is it the filr_managed table? I apologize, I'm on my phone and will try to answer more fully when I sit down at a computer) just stores things like the file id (fid) and the path to the file.
As I understand it, storing the files themselves, fully in the DB would lead to some pretty heavy/intense DB call times.
I'm not sure if this is a reason why you expect it to be in the DB, but if you are looking for the files to be accessible following their nodes access, you'll want to look into setting up private file storage outside of the Drupal root directory.
If you have a default configuration, you will find your files in the filesystem, in the following subdirectory of your Drupal installation :
drupal/sites/default/files
If you want to store all the file in the DB, tou can by using a hook on saving file, but it is not recommended for performance reason. Better to acces a file by URL than download it from database.

Server backend: how to generate file paths for uploaded files?

I am trying to create a site where users can upload images, videos and other types of files.
I did some research and people seem to suggest that saving the files as BLOB in database is a Bad idea; instead, save the file paths in database.
My questions are, if I save the file paths in a database:
1. How do I generate the file names?
I thought about computing the MD5 value of the file name, but what if two files have the same name? Adding the username and time-stamp etc. to file name? Does it even make sense?
2. What is the best directory structure?
If a user uploads images at 12/17/2013, 12/18/2018, can I just put it in user_ABC/images/, then create time-stamped sub-directories 20131217, 20131218 etc. ? What is the best structure for all these stuff?
3. How do all these come together?
It seems like maintaining this system is such a pain, because the file system manipulation scripts are tightly coupled with the database operations(may also need the worry about database transactions? Say in one transaction I updated the database but failed to modify the file system so I need to roll back my database?).
And I think this system doesn't scale (what if my machine runs out of hard disk so I need to upload the files to a second machine? What if my contents are on a cluster?)
I think my real question is:
4. Is there any existing framework/design pattern/db that handles this problem?
What is the standard way of handling this kind of problems?
Thanks in advance for your answers.
I've actually asked this same question when I was designing a social website for food chefs. I decided to store the url of the image in a MySQL database along with recipe. If you plan on storing multiple images for one recipe, in my example, maybe having a comma separated value would work. When the recipe loaded on the page, I would fetch the image associated with that recipe onto the screen.
Since it was a hackathon and wasn't meant for production purposes, I didn't encode the file name into something unique. However, if I were developing for productional purposes, I would append the time-stamp to the media file name when storing it into the server and database/backend.
I believe what I've proposed is the best data structure of handling this scenario. Storing the image onto the server is not only faster, but it should also take less space. I have found that when converting a standard jpg file of reasonable resolution to base64 encoding, the encoded text file representation took 30% more space. There is also the time of encoding the file and decoding the file for storage and resolving when using some BLOB type of data format instead of straight up storing the file on the server.
Using some sort of backend server scripting like PHP, you'll be able to do some pretty neat stuff with the information you have available. Fetch the result from the database, and load it in from the page using HTML.
As far as I know, there isn't a standard way of fetching media from a database yet. Perhaps there will be one day.
There is not standard way to do that, it is different to the different application. The idea is you need generate a different Path+FileName for every upload, here is a way:
HashId = sha1(microsecond + random(1,1000000));
Path = /[user_id]/[HashId{0,2}]/[HashId{-2}];
FileName = HashId

Can I read AND write to a db in my main bundle using core data?

With the particular app I am working on, I have a significant amount of data that I need to have in my db so I can read it in. I also have the need to write a few things to the db. I took a copy of the sqlite db out of the documents folder and put it into my main bundle and can read my manually inserted data without problems.
I am now trying to insert data, but I am running into difficulty. I remember reading somewhere that you can't write to a db in the main bundle? Only the documents folder? Is that correct? What are my options if I need to have custom data in a core data db that I also need write to?
Should I move it out of the main into the documents folder?
Thanks!
I can't find documentation to back this up, but it is my understanding that the application bundle is read-only. I have read that if you have a pre-populated Core Data store in the app bundle, you need to copy it to the Documents directory - and then make modifications that copy.
Check out this.

Best methodology for file uploading (how does YouTube do it?)

What is the best method to follow for uploading files to server using my website? Each user will upload some files to his profile. Should I place all files in a single directory or do I need to create folders for each user and keep the files there?
How does YouTube do this? How should I store the uploaded info in database? What's the most efficient approach to do this when handling large number of users and files? I don't want to know about the usage of API. I want to know the best approach for file organization.
I would have a folder for each user, its more managable!
I wouldn't fancy a massive folder with loads of files!
You could end up with duplicate file names and end up deleting another users files!
I would create a directory for every user!

Resources