Code

Refactored CGI file serving so that FileClass contents are a) read more
[roundup.git] / roundup / backends / blobfiles.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 #$Id: blobfiles.py,v 1.10 2003-12-05 03:28:38 richard Exp $
19 '''
20 This module exports file storage for roundup backends.
21 Files are stored into a directory hierarchy.
22 '''
24 import os
26 def files_in_dir(dir):       
27     if not os.path.exists(dir):
28         return 0
29     num_files = 0
30     for dir_entry in os.listdir(dir):
31         full_filename = os.path.join(dir,dir_entry)
32         if os.path.isfile(full_filename):
33             num_files = num_files + 1
34         elif os.path.isdir(full_filename):
35             num_files = num_files + files_in_dir(full_filename)
36     return num_files
38 class FileStorage:
39     """Store files in some directory structure"""
40     def filename(self, classname, nodeid, property=None):
41         '''Determine what the filename for the given node and optionally 
42            property is.
43         '''
44         if property:
45             name = '%s%s.%s'%(classname, nodeid, property)
46         else:
47             # roundupdb.FileClass never specified the property name, so don't 
48             # include it
49             name = '%s%s'%(classname, nodeid)
51         # have a separate subdir for every thousand messages
52         subdir = str(int(nodeid) / 1000)
53         return os.path.join(self.dir, 'files', classname, subdir, name)
55     def filename_flat(self, classname, nodeid, property=None):
56         '''Determine what the filename for the given node and optionally 
57            property is.
58         '''
59         if property:
60             return os.path.join(self.dir, 'files', '%s%s.%s'%(classname,
61                 nodeid, property))
62         else:
63             # roundupdb.FileClass never specified the property name, so don't 
64             # include it
65             return os.path.join(self.dir, 'files', '%s%s'%(classname,
66                 nodeid))
68     def storefile(self, classname, nodeid, property, content):
69         '''Store the content of the file in the database. The property may be
70            None, in which case the filename does not indicate which property
71            is being saved.
72         '''
73         # determine the name of the file to write to
74         name = self.filename(classname, nodeid, property)
76         # make sure the file storage dir exists
77         if not os.path.exists(os.path.dirname(name)):
78             os.makedirs(os.path.dirname(name))
80         # open the temp file for writing
81         open(name + '.tmp', 'wb').write(content)
83         # save off the commit action
84         self.transactions.append((self.doStoreFile, (classname, nodeid,
85             property)))
87     def getfile(self, classname, nodeid, property):
88         '''Get the content of the file in the database.
89         '''
90         # try a variety of different filenames - the file could be in the
91         # usual place, or it could be in a temp file pre-commit *or* it
92         # could be in an old-style, backwards-compatible flat directory
93         filename = self.filename(classname, nodeid, property)
94         flat_filename = self.filename_flat(classname, nodeid, property)
95         for filename in (filename, filename+'.tmp', flat_filename):
96             if os.path.exists(filename):
97                 f = open(filename, 'rb')
98                 break
99         else:
100             raise IOError, 'content file not found'
101         # snarf the contents and make sure we close the file
102         content = f.read()
103         f.close()
104         return content
106     def numfiles(self):
107         '''Get number of files in storage, even across subdirectories.
108         '''
109         files_dir = os.path.join(self.dir, 'files')
110         return files_in_dir(files_dir)
112     def doStoreFile(self, classname, nodeid, property, **databases):
113         '''Store the file as part of a transaction commit.
114         '''
115         # determine the name of the file to write to
116         name = self.filename(classname, nodeid, property)
118         # the file is currently ".tmp" - move it to its real name to commit
119         os.rename(name+".tmp", name)
121         # return the classname, nodeid so we reindex this content
122         return (classname, nodeid)
124     def rollbackStoreFile(self, classname, nodeid, property, **databases):
125         '''Remove the temp file as a part of a rollback
126         '''
127         # determine the name of the file to delete
128         name = self.filename(classname, nodeid, property)
129         if os.path.exists(name+".tmp"):
130             os.remove(name+".tmp")
132 # vim: set filetype=python ts=4 sw=4 et si