openmedialibrary/oml/item/icons.py

190 lines
5 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
2014-08-12 08:16:57 +00:00
import sqlite3
2014-05-19 21:39:52 +00:00
2014-08-12 08:16:57 +00:00
import tornado.concurrent
import tornado.gen
2014-05-19 21:39:52 +00:00
import tornado.ioloop
import tornado.web
from oxtornado import run_async
from settings import icons_db_path, static_path
2014-08-12 08:16:57 +00:00
from utils import resize_image
2014-08-09 16:14:14 +00:00
import db
2014-05-04 17:26:43 +00:00
2014-05-21 00:02:21 +00:00
import logging
2015-11-29 14:56:38 +00:00
logger = logging.getLogger(__name__)
2014-05-21 00:02:21 +00:00
2014-08-12 08:16:57 +00:00
2014-05-21 00:02:21 +00:00
class Icons(dict):
2014-05-04 17:26:43 +00:00
def __init__(self, db):
self._db = db
2014-05-17 09:24:17 +00:00
self.create()
2014-05-04 17:26:43 +00:00
def connect(self):
2014-05-17 09:24:17 +00:00
conn = sqlite3.connect(self._db, timeout=10)
return conn
2014-05-04 17:26:43 +00:00
def create(self):
2014-05-17 09:24:17 +00:00
conn = self.connect()
c = conn.cursor()
2014-09-02 22:32:44 +00:00
c.execute('CREATE TABLE IF NOT EXISTS icon (id varchar(64) unique, data blob)')
c.execute('CREATE TABLE IF NOT EXISTS setting (key varchar(256) unique, value text)')
2014-05-04 17:26:43 +00:00
if int(self.get_setting(c, 'version', 0)) < 1:
self.set_setting(c, 'version', 1)
def get_setting(self, c, key, default=None):
2014-09-02 22:32:44 +00:00
c.execute('SELECT value FROM setting WHERE key = ?', (key, ))
2014-05-04 17:26:43 +00:00
for row in c:
return row[0]
return default
def set_setting(self, c, key, value):
2014-09-02 22:32:44 +00:00
c.execute('INSERT OR REPLACE INTO setting values (?, ?)', (key, str(value)))
2014-05-04 17:26:43 +00:00
def default_cover(self):
with open(os.path.join(static_path, 'png', 'cover.png'), 'rb') as f:
data = f.read()
2014-05-04 17:26:43 +00:00
return data
def __getitem__(self, id, default=None):
2014-09-02 22:32:44 +00:00
sql = 'SELECT data FROM icon WHERE id=?'
2014-05-17 09:24:17 +00:00
conn = self.connect()
c = conn.cursor()
2014-05-04 17:26:43 +00:00
c.execute(sql, (id, ))
data = default
for row in c:
data = row[0]
break
c.close()
2014-05-17 09:24:17 +00:00
conn.close()
2014-05-04 17:26:43 +00:00
return data
def __setitem__(self, id, data):
2014-09-02 22:32:44 +00:00
sql = 'INSERT OR REPLACE INTO icon values (?, ?)'
2014-05-17 09:24:17 +00:00
conn = self.connect()
c = conn.cursor()
2014-05-04 17:26:43 +00:00
data = sqlite3.Binary(data)
c.execute(sql, (id, data))
2014-05-17 09:24:17 +00:00
conn.commit()
2014-05-04 17:26:43 +00:00
c.close()
2014-05-17 09:24:17 +00:00
conn.close()
2014-05-04 17:26:43 +00:00
def __delitem__(self, id):
2014-09-02 22:32:44 +00:00
sql = 'DELETE FROM icon WHERE id = ?'
2014-05-17 09:24:17 +00:00
conn = self.connect()
c = conn.cursor()
2014-05-04 17:26:43 +00:00
c.execute(sql, (id, ))
2014-05-17 09:24:17 +00:00
conn.commit()
2014-05-04 17:26:43 +00:00
c.close()
2014-05-17 09:24:17 +00:00
conn.close()
2014-05-04 17:26:43 +00:00
2016-01-16 05:17:52 +00:00
def clear(self, prefix):
sql = 'DELETE FROM icon WHERE id LIKE ?'
conn = self.connect()
c = conn.cursor()
c.execute(sql, (prefix + '%', ))
conn.commit()
c.close()
conn.close()
2014-05-21 00:02:21 +00:00
icons = Icons(icons_db_path)
2014-05-19 21:39:52 +00:00
@run_async
2014-08-09 17:06:06 +00:00
def get_icon(id, type_, size, callback):
2014-05-26 10:11:13 +00:00
if size:
skey = '%s:%s:%s' % (type_, id, size)
data = icons[skey]
if data:
2014-09-02 22:32:44 +00:00
callback(bytes(data))
2014-05-26 10:11:13 +00:00
return
key = '%s:%s' % (type_, id)
2014-05-27 09:59:23 +00:00
data = icons[key]
if not data:
2014-05-26 10:11:13 +00:00
type_ = 'preview' if type_ == 'cover' else 'cover'
2014-05-27 09:59:23 +00:00
key = '%s:%s' % (type_, id)
2014-05-26 10:11:13 +00:00
if size:
skey = '%s:%s:%s' % (type_, id, size)
if size:
data = icons[skey]
if data:
size = None
if not data:
data = icons[key]
if not data:
skey = '%s:%s:%s' % ('default', 'cover', size)
if size:
data = icons[skey]
if data:
size = None
if not data:
data = icons.default_cover()
if size:
data = icons[skey] = resize_image(data, size=size)
2014-05-26 10:11:13 +00:00
size = None
if size:
data = icons[skey] = resize_image(data, size=size)
2014-09-02 22:32:44 +00:00
data = bytes(data) or ''
2014-05-26 10:11:13 +00:00
callback(data)
def clear_default_cover_cache():
2016-01-16 05:17:52 +00:00
icons.clear('default:cover:')
2014-05-26 10:11:13 +00:00
@run_async
2014-08-09 17:06:06 +00:00
def get_icon_app(id, type_, size, callback):
2014-08-09 16:14:14 +00:00
with db.session():
2014-05-19 21:39:52 +00:00
from item.models import Item
item = Item.get(id)
if not item:
2015-12-24 15:11:47 +00:00
data = ''
2014-05-19 22:49:04 +00:00
else:
2014-05-21 00:02:21 +00:00
if type_ == 'cover' and not item.meta.get('cover'):
type_ = 'preview'
if type_ == 'preview' and not item.files.count():
type_ = 'cover'
if size:
skey = '%s:%s:%s' % (type_, id, size)
key = '%s:%s' % (type_, id)
2014-05-19 22:49:04 +00:00
data = None
if size:
2014-05-21 00:02:21 +00:00
data = icons[skey]
2014-05-19 22:49:04 +00:00
if data:
size = None
if not data:
2014-05-21 00:02:21 +00:00
data = icons[key]
2014-05-19 21:39:52 +00:00
if not data:
data = icons.default_cover()
2014-05-21 00:02:21 +00:00
size = None
2014-05-19 22:49:04 +00:00
if size:
2014-05-21 00:02:21 +00:00
data = icons[skey] = resize_image(data, size=size)
2014-09-02 22:32:44 +00:00
data = bytes(data) or ''
2015-12-24 15:11:47 +00:00
callback(data)
2014-05-19 21:39:52 +00:00
2014-05-21 00:02:21 +00:00
class IconHandler(tornado.web.RequestHandler):
2014-05-19 21:39:52 +00:00
2014-08-09 17:06:06 +00:00
def initialize(self):
pass
2014-05-19 21:39:52 +00:00
@tornado.web.asynchronous
@tornado.gen.coroutine
2014-05-21 00:02:21 +00:00
def get(self, id, type_, size=None):
size = int(size) if size else None
if type_ not in ('cover', 'preview'):
2014-05-25 20:32:00 +00:00
self.set_status(404)
2014-05-21 00:02:21 +00:00
return
self.set_header('Content-Type', 'image/jpeg')
2014-08-09 17:06:06 +00:00
response = yield tornado.gen.Task(get_icon, id, type_, size)
2014-05-21 00:02:21 +00:00
if not response:
2014-05-25 20:32:00 +00:00
self.set_status(404)
2014-05-21 00:02:21 +00:00
return
if self._finished:
return
self.write(response)