make preview available to peers
This commit is contained in:
parent
8e3d8b5f8e
commit
3339c641c1
2 changed files with 59 additions and 6 deletions
|
@ -109,7 +109,12 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
import item.models
|
import item.models
|
||||||
id = self.path.split('/')[-1] if self.path.startswith('/get/') else None
|
parts = self.path.split('/')
|
||||||
|
if len(parts) == 3 and parts[1] in ('get', 'preview'):
|
||||||
|
id = parts[2]
|
||||||
|
preview = parts[1] == 'preview'
|
||||||
|
else:
|
||||||
|
id = None
|
||||||
if id and len(id) == 32 and id.isalnum():
|
if id and len(id) == 32 and id.isalnum():
|
||||||
with db.session():
|
with db.session():
|
||||||
i = item.models.Item.get(id)
|
i = item.models.Item.get(id)
|
||||||
|
@ -119,6 +124,21 @@ class Handler(http.server.SimpleHTTPRequestHandler):
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(b'404 - Not Found')
|
self.wfile.write(b'404 - Not Found')
|
||||||
return
|
return
|
||||||
|
if preview:
|
||||||
|
from item.icons import icons
|
||||||
|
key = 'preview:' + id
|
||||||
|
data = icons[key]
|
||||||
|
if data:
|
||||||
|
self.send_response(200, 'ok')
|
||||||
|
self.send_header('Content-type', 'image/jpg')
|
||||||
|
else:
|
||||||
|
self.send_response(404, 'Not Found')
|
||||||
|
self.send_header('Content-type', 'text/plain')
|
||||||
|
data = b'404 - Not Found'
|
||||||
|
self.send_header('Content-Length', str(len(data)))
|
||||||
|
self.end_headers()
|
||||||
|
self.wfile.write(data)
|
||||||
|
return
|
||||||
path = i.get_path()
|
path = i.get_path()
|
||||||
mimetype = {
|
mimetype = {
|
||||||
'epub': 'application/epub+zip',
|
'epub': 'application/epub+zip',
|
||||||
|
|
43
oml/nodes.py
43
oml/nodes.py
|
@ -304,17 +304,19 @@ class Node(Thread):
|
||||||
trigger_event('peering.%s'%action.replace('Peering', ''), u.json())
|
trigger_event('peering.%s'%action.replace('Peering', ''), u.json())
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'X-Node-Protocol': settings.NODE_PROTOCOL,
|
||||||
|
'User-Agent': settings.USER_AGENT,
|
||||||
|
'Accept-Encoding': 'gzip',
|
||||||
|
}
|
||||||
|
|
||||||
def download(self, item):
|
def download(self, item):
|
||||||
from item.models import Transfer
|
from item.models import Transfer
|
||||||
self.resolve()
|
self.resolve()
|
||||||
url = '%s/get/%s' % (self.url, item.id)
|
url = '%s/get/%s' % (self.url, item.id)
|
||||||
headers = {
|
|
||||||
'X-Node-Protocol': settings.NODE_PROTOCOL,
|
|
||||||
'User-Agent': settings.USER_AGENT,
|
|
||||||
}
|
|
||||||
t1 = datetime.utcnow()
|
t1 = datetime.utcnow()
|
||||||
logger.debug('download %s', url)
|
logger.debug('download %s', url)
|
||||||
self._opener.addheaders = list(zip(headers.keys(), headers.values()))
|
self._opener.addheaders = list(zip(self.headers.keys(), self.headers.values()))
|
||||||
try:
|
try:
|
||||||
r = self._opener.open(url, timeout=self.TIMEOUT*2)
|
r = self._opener.open(url, timeout=self.TIMEOUT*2)
|
||||||
except:
|
except:
|
||||||
|
@ -363,6 +365,37 @@ class Node(Thread):
|
||||||
logger.debug('FAILED %s', url)
|
logger.debug('FAILED %s', url)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def download_preview(self, item):
|
||||||
|
from item.icons import icons
|
||||||
|
self.resolve()
|
||||||
|
url = '%s/preview/%s' % (self.url, item.id)
|
||||||
|
self._opener.addheaders = list(zip(self.headers.keys(), self.headers.values()))
|
||||||
|
try:
|
||||||
|
r = self._opener.open(url, timeout=self.TIMEOUT*2)
|
||||||
|
except:
|
||||||
|
logger.debug('openurl failed %s', url, exc_info=1)
|
||||||
|
return False
|
||||||
|
code = r.getcode()
|
||||||
|
if code == 200:
|
||||||
|
try:
|
||||||
|
fileobj = r
|
||||||
|
if r.headers.get('content-encoding', None) == 'gzip':
|
||||||
|
fileobj = gzip.GzipFile(fileobj=r)
|
||||||
|
content = fileobj.read()
|
||||||
|
key = 'preview:' + item.id
|
||||||
|
icons[key] = content
|
||||||
|
for resolution in (128, 256, 512):
|
||||||
|
del icons['preview:%s' % resolution]
|
||||||
|
return True
|
||||||
|
except:
|
||||||
|
logger.debug('preview download failed %s', url, exc_info=1)
|
||||||
|
return False
|
||||||
|
elif code == 404:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
logger.debug('FAILED %s', url)
|
||||||
|
return False
|
||||||
|
|
||||||
def download_upgrade(self, release):
|
def download_upgrade(self, release):
|
||||||
for module in release['modules']:
|
for module in release['modules']:
|
||||||
path = os.path.join(settings.update_path, release['modules'][module]['name'])
|
path = os.path.join(settings.update_path, release['modules'][module]['name'])
|
||||||
|
|
Loading…
Reference in a new issue