Because "file system" and "rails" are such common topics both together and separate I fail to find any Ruby on Rails open source app that implements a file system in the database. I would like to use such an application as a starting point or template.
I've already been able to implement the User
and the Directory
models (using Ancestry for the latter), and I'm on my way for the File
model (my app only requires one kind of file).
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
has_many :directories, dependent: :destroy
# ...
end # class User
class Directory < ActiveRecord::Base
attr_accessible :name, :parent_id
has_ancestry
belongs_to :user
has_many :files, dependent: :destroy
# ...
end # class Directory
# not actually implemented, yet
class File < ActiveRecord::Base
attr_accessible :name
belongs_to :directory
# ...
end # class File
In views I'm using jsTree to present the tree and a form to add/delete, edit, ... This will need to change into using AJAX because redirecting back to same page does not preserve the expanded/collapsed state of the tree.
However I have this nagging feeling that I'm doing something that has already been done lots of times. Can you please provide links to such application(s) or give hints about implementing both the model part and the view part?
To get model to be organised as a tree structure the tecnique is know as Nested set model therefore a common name (helpful to googling etc. ) could be "Activerecord nesting" ;-)
Your choice about Ancestry is welcome but you can benefit having a look at projects (mix-in,plug-in,...) like:
For the file upload 'n store part I would suggest , in addition to the already mentioned Paperclip, to look at carrierwave by itself provides a storage based on the "fog" gem (supports storing files with AWS, Google, Local and Rackspace ) but you can opt for database (e.g. sqlite) storage leveraging carrierwave-activerecord
About "views" you might be interested in this answer about jQuery File Tree a configurable AJAX file browser plugin for jQuery and dnamique blog which has a rails connector for this plugin and sources and demo about it.
as an alternative, look at the implementation (sources) of the applications mentioned in next section.
Here some "File manager" of interest:
I think you're on the right track. Your Directory
and File
models look fine to me.
Your nagging feeling is partly correct. It's a common requirement to support uploading and storing files, but it's not that common to model and display an entire hierarchal directory structure.
You may want to reconsider actually storing the files in the database. This is usually a bad idea. Since files are such variable sizes, they can bloat your table and hurt performance. I recommend storing your files in Amazon S3. This is much more reliable and fast storage, and you can serve S3 urls directly to clients to reduce bandwidth and load on your own servers. You can use the paperclip gem to handle file uploads and store the files either on disk or on S3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With