1 # DeGAL - A pretty simple web image gallery |
|
2 # Copyright (C) 2007 Tero Marttila |
|
3 # http://marttila.de/~terom/degal/ |
|
4 # |
|
5 # This program is free software; you can redistribute it and/or modify |
|
6 # it under the terms of the GNU General Public License as published by |
|
7 # the Free Software Foundation; either version 2 of the License, or |
|
8 # (at your option) any later version. |
|
9 # |
|
10 # This program is distributed in the hope that it will be useful, |
|
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 # GNU General Public License for more details. |
|
14 # |
|
15 # You should have received a copy of the GNU General Public License |
|
16 # along with this program; if not, write to the |
|
17 # Free Software Foundation, Inc., |
|
18 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
19 # |
|
20 |
|
21 import struct |
|
22 import base64 |
|
23 import shelve |
|
24 import os.path |
|
25 |
|
26 |
|
27 import utils, db, helpers, folder, image, log |
|
28 |
|
29 def int2key (id) : |
|
30 """ |
|
31 Turn an integer into a short-as-possible url-safe string |
|
32 """ |
|
33 for type in ('B', 'H', 'I') : |
|
34 try : |
|
35 return base64.b64encode(struct.pack(type, id), '-_').rstrip('=') |
|
36 except struct.error : |
|
37 continue |
|
38 |
|
39 raise Exception("ID overflow: %s" % id) |
|
40 |
|
41 def key2int (key) : |
|
42 # base64 ignores extra padding, but if it doesn't, it's (4 - len%4), if len%4 != 0 |
|
43 # and it breaks on unicode strings |
|
44 bytes = base64.b64decode(str(key + '='*6), '-_') |
|
45 |
|
46 type = { |
|
47 1: 'B', |
|
48 2: 'H', |
|
49 4: 'I', |
|
50 }[len(bytes)] |
|
51 |
|
52 return struct.unpack(type, bytes)[0] |
|
53 |
|
54 class DB (object) : |
|
55 def __init__ (self, read_only=True) : |
|
56 self.db = shelve.open('shorturls2', read_only and 'r' or 'c') |
|
57 |
|
58 def html_path (self, key, index) : |
|
59 type, dirpath, fname = self.db[key] |
|
60 |
|
61 if type == 'img' : |
|
62 fname += '.html' |
|
63 elif type == 'dir' : |
|
64 fname = '' |
|
65 |
|
66 if index : |
|
67 dirpath = '../%s' % dirpath |
|
68 |
|
69 if type == 'dir' and index > 1 : |
|
70 fname = 'index_%s.html' % (index - 1) |
|
71 |
|
72 return os.path.join(dirpath, fname) |
|
73 |
|
74 def image_info (self, key) : |
|
75 type, dirpath, fname = self.db[key] |
|
76 |
|
77 if type != 'img' : |
|
78 raise ValueError("%s is not an img" % key) |
|
79 |
|
80 return dirpath, fname |
|
81 |
|
82 def shorturls_for (self, paths) : |
|
83 ret = [] |
|
84 |
|
85 for key in self.db.keys() : |
|
86 if key.startswith('_') : |
|
87 continue |
|
88 |
|
89 type, dir, fname = self.db[key] |
|
90 path = os.path.join(dir.lstrip('.').lstrip('/'), fname) |
|
91 if path in paths : |
|
92 ret.append(key) |
|
93 paths.remove(path) |
|
94 |
|
95 if paths : |
|
96 raise ValueError("Paths not found: %s" % " ".join(paths)) |
|
97 |
|
98 return ret |
|
99 |
|
100 def html_path (key, index=None) : |
|
101 dir, fname = node_info(key) |
|
102 |
|
103 if fname : |
|
104 return utils.url(dir, fname + '.html') |
|
105 else : |
|
106 return utils.url(dir, helpers.url_for_page(index or 0)) |
|
107 |
|
108 def node_info (key) : |
|
109 res = db.select("""SELECT dirpath, filename FROM nodes WHERE id=?""", key2int(key)).fetchone() |
|
110 |
|
111 if res : |
|
112 return res |
|
113 |
|
114 else : |
|
115 raise KeyError(key) |
|
116 |
|
117 def image_info (key) : |
|
118 res = db.select("""SELECT dirpath, filename FROM images WHERE id=?""", key2int(key)).fetchone() |
|
119 |
|
120 if res : |
|
121 return res |
|
122 |
|
123 else : |
|
124 raise KeyError(key) |
|
125 |
|
126 def get_images (keys) : |
|
127 res = [db.select("""SELECT dirpath, filename FROM images WHERE id=?""", key2int(key)).fetchone() for key in keys] |
|
128 |
|
129 # don't mind if we don't get as many as we asked for? |
|
130 if res : |
|
131 return res |
|
132 |
|
133 else : |
|
134 raise KeyError(keys) |
|
135 |
|
136 def _got_obj_key (obj, id) : |
|
137 key = int2key(id) |
|
138 |
|
139 obj.shorturl_code = key |
|
140 |
|
141 if isinstance(obj, folder.Folder) : |
|
142 dir, fname = utils.strip_path(obj.path), '' |
|
143 elif isinstance(obj, image.Image) : |
|
144 dir, fname = utils.strip_path(obj.dir.path), obj.name |
|
145 else : |
|
146 assert(False, "%r %r" % (obj, id)) |
|
147 |
|
148 log.info("%6s -> %s/%s", key, dir, fname) |
|
149 |
|
150 def updateDB (root) : |
|
151 """ |
|
152 Update the SQL database |
|
153 |
|
154 type - one of 'img', 'dir' |
|
155 dirpath - the path to the directory, e.g. '.', './foobar', './foobar/quux' |
|
156 fname - the filename, one of '', 'DSC9839.JPG', 'this.png', etc. |
|
157 """ |
|
158 |
|
159 dirqueue = [root] |
|
160 |
|
161 # dict of (dir, fname) -> obj |
|
162 paths = {} |
|
163 |
|
164 while dirqueue : |
|
165 dir = dirqueue.pop(0) |
|
166 |
|
167 dirqueue.extend(dir.subdirs.itervalues()) |
|
168 |
|
169 if dir.alive : |
|
170 pathtuple = (utils.strip_path(dir.path), '') |
|
171 |
|
172 log.debug("dir %50s", pathtuple[0]) |
|
173 |
|
174 paths[pathtuple] = dir |
|
175 |
|
176 for img in dir.images.itervalues() : |
|
177 pathtuple = (utils.strip_path(img.dir.path), img.name) |
|
178 |
|
179 log.debug("img %50s %15s", *pathtuple) |
|
180 |
|
181 paths[pathtuple] = img |
|
182 |
|
183 log.info("we have %d nodes", len(paths)) |
|
184 |
|
185 for (id, dir, fname) in db.select("SELECT id, dirpath, filename FROM nodes") : |
|
186 try : |
|
187 obj = paths.pop((dir, fname)) |
|
188 key = int2key(id) |
|
189 |
|
190 obj.shorturl_code = key |
|
191 |
|
192 log.debug("%s %50s %15s -> %d %s", dir and "img" or "dir", dir, fname, id, key) |
|
193 |
|
194 except KeyError : |
|
195 pass |
|
196 # log.warning("non-existant node (%d, %s, %s) in db", id, dir, fname) |
|
197 |
|
198 if paths : |
|
199 log.info("allocating shorturls for %d new nodes:", len(paths)) |
|
200 |
|
201 db.insert_many( |
|
202 _got_obj_key, |
|
203 "INSERT INTO nodes (dirpath, filename) VALUES (?, ?)", |
|
204 ((obj, (path, fname)) for ((path, fname), obj) in paths.iteritems()) |
|
205 ) |
|
206 else : |
|
207 log.info("no new images") |
|
208 |
|