Browse Source

Add thumbs generation

master
Dashie der otter 4 years ago
parent
commit
4dae8ba91a
5 changed files with 36 additions and 5 deletions
  1. 1
    0
      .gitignore
  2. 2
    2
      controllers/parts.py
  3. 31
    2
      crons.py
  4. 2
    1
      stockazng.py
  5. 0
    0
      uploads/thumbs/.gitkeep

+ 1
- 0
.gitignore View File

@@ -13,3 +13,4 @@ StockazNG.komodoproject
uploads/barcodes/*
uploads/attachments/*
uploads/cache/*
uploads/thumbs/*

+ 2
- 2
controllers/parts.py View File

@@ -162,9 +162,9 @@ def parts_attachments_dl(pid, attachid):
ftype = 'attachments'
if attach.is_remote:
ftype = 'cache'
fname = path_or_none(fbase, ftype, attach.filename)
fname = path_or_none(fbase, ftype, os.path.join(str(Part.BARCODE_TYPE), attach.filename))
if not fname:
return redirect(url_for('bp_parts.parts_edit', pid=pid, _anchor="AddPartTabAttachments"))

return send_from_directory(os.path.join(fbase, ftype), attach.filename, as_attachment=True)
return send_from_directory(os.path.join(fbase, ftype, str(Part.BARCODE_TYPE)), attach.filename, as_attachment=True)


+ 31
- 2
crons.py View File

@@ -8,6 +8,7 @@ import urlparse
from datetime import datetime
from hashlib import sha1
import magic
from PIL import Image

logger = logging.getLogger()

@@ -18,6 +19,26 @@ TRIANGLE = {
}


def illuminatis(cfg, file, size, logger):
infile = os.path.join(cfg['UPLOADS_DEFAULT_DEST'],
'attachments',
str(Part.BARCODE_TYPE),
file.filename)
outfile = os.path.join(cfg['UPLOADS_DEFAULT_DEST'],
'thumbs',
str(Part.BARCODE_TYPE),
"thumb_%s.%s" % (size, file.filename))

if not os.path.isdir(os.path.join(cfg['UPLOADS_DEFAULT_DEST'], 'thumbs', str(Part.BARCODE_TYPE))):
os.makedirs(os.path.join(cfg['UPLOADS_DEFAULT_DEST'], 'thumbs', str(Part.BARCODE_TYPE)))
if not os.path.isfile(outfile) and os.path.isfile(infile):
im = Image.open(infile)
im.thumbnail(TRIANGLE[size], Image.ANTIALIAS)
im.save(outfile)
logger.info("[cron_thumbs] Created %s(%s) thumb for %s" % (size, TRIANGLE[size], file.filename))


def _cron_thumbs(config, db):
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
logger.setLevel(logging.DEBUG)
@@ -32,7 +53,15 @@ def _cron_thumbs(config, db):

logger.info("Started creating thumbnails.")

pass
images = PartAttachment.query.filter(PartAttachment.simple_type == "IMAGE").all()

for i in images:
logger.info("[{0}]: {1}".format(i.id, i.orig_filename))
illuminatis(config, i, "small", logger)
illuminatis(config, i, "medium", logger)
illuminatis(config, i, "preview", logger)

logger.info("Finished creating thumbnails.")


def _cron_cache(config, db):
@@ -82,7 +111,7 @@ def _cron_cache(config, db):
os.path.join(dir_dest, new_name))

i.orig_filename = orig_name
i.filename = "{0}/{1}".format(Part.BARCODE_TYPE, new_name)
i.filename = new_name

_magic = magic.from_file(os.path.join(dir_dest, new_name), mime=True).decode('unicode_escape')


+ 2
- 1
stockazng.py View File

@@ -27,6 +27,7 @@ from dbseed import make_db_seed
from forms import ExtendedRegisterForm

from crons import _cron_cache, _cron_thumbs
from posixpath import basename

from datetime import datetime
from hashlib import sha1
@@ -117,7 +118,7 @@ def parts_attachment_new(pid):
else:
st = "UNKNOWN"

pa = PartAttachment(filename=filename,
pa = PartAttachment(filename=basename(filename),
orig_filename=request.files['attachment'].filename,
mimetype=_magic,
simple_type=st,

+ 0
- 0
uploads/thumbs/.gitkeep View File