--- a/lib/shorturl.py Wed Jan 16 16:28:03 2008 +0000
+++ b/lib/shorturl.py Wed Jan 16 18:44:03 2008 +0000
@@ -25,6 +25,8 @@
from log import index
+import utils, db
+
def int2key (id) :
"""
Turn an integer into a short-as-possible url-safe string
@@ -37,6 +39,18 @@
raise Exception("ID overflow: %s" % id)
+def key2int (key) :
+ # base64 ignores extra padding, but if it doesn't, it's (4 - len%4), if len%4 != 0
+ bytes = base64.b64decode(key + '='*6, '-_')
+
+ type = {
+ 1: 'B',
+ 2: 'H',
+ 4: 'I',
+ }[len(bytes)]
+
+ return struct.unpack(type, bytes)[0]
+
def updateDB (root) :
"""
DeGAL <= 0.2 used a simple key => path mapping, but now we use
@@ -143,3 +157,27 @@
return ret
+def html_path (key, index=None) :
+ dir, fname = image_info(key)
+
+ return utils.url(dir, fname + '.html')
+
+def image_info (key) :
+ res = db.select("""SELECT dirpath, filename FROM images WHERE id=?""", key2int(key)).fetchone()
+
+ if res :
+ return res
+
+ else :
+ raise KeyError(key)
+
+def get_images (keys) :
+ res = [db.select("""SELECT dirpath, filename FROM images WHERE id=?""", key2int(key)).fetchone() for key in keys]
+
+ # don't mind if we don't get as many as we asked for?
+ if res :
+ return res
+
+ else :
+ raise KeyError(keys)
+