Hook->hooks since there's more than one of them :)
[mediagoblin.git] / docs / source / devel / storage.rst
CommitLineData
340100ee
E
1=========
2 Storage
3=========
4
340100ee
E
5The storage systems attached to your app
6----------------------------------------
7
8Dynamic content: queue_store and public_store
9~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10
a2d94b0c 11Two instances of the StorageInterface come attached to your app. These
12are:
13
90e7fc67
CAW
14+ **queue_store:** When a user submits a fresh piece of media for
15 their gallery, before the Processing stage, that piece of media sits
16 here in the queue_store. (It's possible that we'll rename this to
17 "private_store" and start storing more non-publicly-stored stuff in
18 the future...). This is a StorageInterface implementation
19 instance. Visitors to your site probably cannot see it... it isn't
20 designed to be seen, anyway.
21
22+ **public_store:** After your media goes through processing it gets
23 moved to the public store. This is also a StorageInterface
24 implelementation, and is for stuff that's intended to be seen by
25 site visitors.
a2d94b0c 26
340100ee
E
27The workbench
28~~~~~~~~~~~~~
29
30In addition, there's a "workbench" used during
31processing... it's just for temporary files during
32processing, and also for making local copies of stuff that
33might be on remote storage interfaces while transitionally
34moving/converting from the queue_store to the public store.
35See the workbench module documentation for more.
36
37.. automodule:: mediagoblin.tools.workbench
38 :members:
39 :show-inheritance:
40
41
42Static assets / staticdirect
43~~~~~~~~~~~~~~~~~~~~~~~~~~~~
44
a2d94b0c 45On top of all that, there is some static media that comes bundled with your
46application. This stuff is kept in:
47
48 mediagoblin/static/
49
50These files are for mediagoblin base assets. Things like the CSS files,
51logos, etc. You can mount these at whatever location is appropriate to you
52(see the direct_remote_path option in the config file) so if your users
53are keeping their static assets at http://static.mgoblin.example.org/ but
54their actual site is at http://mgoblin.example.org/, you need to be able
55to get your static files in a where-it's-mounted agnostic way. There's a
56"staticdirector" attached to the request object. It's pretty easy to use;
57just look at this bit taken from the
58mediagoblin/templates/mediagoblin/base.html main template:
59
60 <link rel="stylesheet" type="text/css"
61 href="Template:Request.staticdirect('/css/extlib/text.css')"/>
62
63see? Not too hard. As expected, if you configured direct_remote_path to be
64http://static.mgoblin.example.org/ you'll get back
65http://static.mgoblin.example.org/css/extlib/text.css just as you'd
66probably expect.
340100ee
E
67
68StorageInterface and implementations
69------------------------------------
70
71The guts of StorageInterface and friends
72~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73
a2d94b0c 74So, the StorageInterface!
75
76So, the public and queue stores both use StorageInterface implementations
77... but what does that mean? It's not too hard.
78
79Open up:
80
81 mediagoblin/storage.py
82
83In here you'll see a couple of things. First of all, there's the
84StorageInterface class. What you'll see is that this is just a very simple
85python class. A few of the methods actually implement things, but for the
86most part, they don't. What really matters about this class is the
87docstrings. Each expected method is documented as to how it should be
88constructed. Want to make a new StorageInterface? Simply subclass it. Want
89to know how to use the methods of your storage system? Read these docs,
90they span all implementations.
91
92There are a couple of implementations of these classes bundled in
93storage.py as well. The most simple of these is BasicFileStorage, which is
94also the default storage system used. As expected, this stores files
95locally on your machine.
96
97There's also a CloudFileStorage system. This provides a mapping to
98[OpenStack's swift http://swift.openstack.org/] storage system (used by
99RackSpace Cloud files and etc).
100
101Between these two examples you should be able to get a pretty good idea of
102how to write your own storage systems, for storing data across your
103beowulf cluster of radioactive monkey brains, whatever.
104
340100ee
E
105Writing code to store stuff
106~~~~~~~~~~~~~~~~~~~~~~~~~~~
a2d94b0c 107
108So what does coding for StorageInterface implementations actually look
109like? It's pretty simple, really. For one thing, the design is fairly
110inspired by [Django's file storage API
111https://docs.djangoproject.com/en/dev/ref/files/storage/]... with some
112differences.
113
114Basically, you access files on "file paths", which aren't exactly like
115unix file paths, but are close. If you wanted to store a file on a path
116like dir1/dir2/filename.jpg you'd actually write that file path like:
117
118['dir1', 'dir2', 'filename.jpg']
119
120This way we can be *sure* that each component is actually a component of
121the path that's expected... we do some filename cleaning on each component.
122
123Your StorageInterface should pass in and out "file like objects". In other
124words, they should provide .read() and .write() at minimum, and probably
125also .seek() and .close().