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