Permalink
Browse files

a very rudimentary thumbnail service, work for #10

  • Loading branch information...
shawnmjones committed Jul 16, 2018
1 parent d8fbfc0 commit fe0c9014e283f9d52d026a802e698781ca2a0f62
View
@@ -9,3 +9,4 @@ dist
.web_cache
*.sqlite
*.bak
node_modules
View
@@ -1,3 +1,6 @@
CACHEMODEL = 'SQLite'
LOGLEVEL = "DEBUG"
REQUEST_TIMEOUT = 15
ENABLE_THUMBNAILS = "Yes"
THUMBNAIL_SCRIPT_PATH = "mementoembed/static/js/create_screenshot.js"
THUMBNAIL_WORKING_FOLDER = "/tmp/mementoembed/thumbnails"
View
Binary file not shown.
@@ -1,10 +1,14 @@
import os
import logging
import json
import subprocess
import traceback
import hashlib
import htmlmin
import requests_cache
from flask import render_template, request, Blueprint, current_app
from flask import render_template, request, Blueprint, current_app, make_response
from mementoembed.mementosurrogate import MementoSurrogate
from mementoembed.cachesession import CacheSession
@@ -69,8 +73,52 @@ def thumbnail_endpoint(subpath):
try:
# TODO: test that subpath is actually a memento
if current_app.config['ENABLE_THUMBNAILS'] == "Yes":
return "The thumbnail service is not yet available", 500
if os.path.isdir(current_app.config['THUMBNAIL_WORKING_FOLDER']):
urim = subpath
os.environ['URIM'] = urim
m = hashlib.sha256()
m.update(urim.encode('utf8'))
thumbfile = m.hexdigest()
thumbfile = "{}/{}.png".format(current_app.config['THUMBNAIL_WORKING_FOLDER'], m.hexdigest())
os.environ['THUMBNAIL_OUTPUTFILE'] = thumbfile
p = subprocess.Popen(["node", current_app.config['THUMBNAIL_SCRIPT_PATH']])
try:
p.wait(timeout=30)
with open(thumbfile, 'rb') as f:
data = f.read()
response = make_response(data)
response.headers['Content-Type'] = 'image/png'
return response, 200
except TimeoutError:
module_logger.exception("Thumbnail script failed to return after 30 seconds")
output = {
"error": "a thumbnail failed to generated in 30 seconds",
"error details": repr(traceback.format_exc())
}
return json.dumps(output), 500
else:
msg = "Thumbnail folder {} does not exist".format(current_app.config['THUMBNAIL_WORKING_FOLDER'])
module_logger.exception(msg)
output = {
"error": msg,
"error details": repr(traceback.format_exc())
}
return json.dumps(output), 500
else:
return "The thumbnail service has been disabled by the system administrator", 200
@@ -0,0 +1,9 @@
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({headless:true});
const page = await browser.newPage();
await page.goto(process.env.URIM);
await page.screenshot({path: process.env.THUMBNAIL_OUTPUTFILE});
await browser.close();
})();
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit fe0c901

Please sign in to comment.