def get(self): size = self._get_size() thumbdir = os.path.join(self.ctx.storage.path, 'thumbnails') f = self.ctx.file # use a lossless format in doubt lossy = f.get_mimetype() == 'image/jpeg' thumbext = 'jpg' if lossy else 'png' thumbpath = os.path.join(thumbdir, str(size), '%s.%s' % (f.get_hash(), thumbext)) f.data['thumbnail']['ext'] = thumbext f.save() mtime = int(os.path.getmtime(f.get_realpath())) if not os.path.exists(thumbpath) or mtime != int(os.path.getmtime(thumbpath)): if not os.path.isdir(os.path.dirname(thumbpath)): os.makedirs(os.path.dirname(thumbpath)) os.chmod(os.path.dirname(thumbpath), 0770) with open(f.get_realpath(), 'rb') as fp: img = Image.open(fp) img.thumbnail((size, size), Image.BILINEAR) with open(thumbpath, 'wb') as tfp: if lossy: img.save(tfp, 'jpeg', quality=95) else: img.save(tfp, 'png') os.utime(thumbpath, (mtime, mtime)) self.ctx.res = FileApp(thumbpath) self.ctx.res.cache_control(private=True, max_age=CACHE_CONTROL.ONE_HOUR) CONTENT_DISPOSITION.apply(self.ctx.res.headers, inline=True, filename="%s_thumb_%s.%s" % (os.path.splitext(f.get_name())[0], size, thumbext))
def get(self): filename = self.ctx.req.GET.get('file') if self.SANITIZE_REGEXP.match(filename): realpath = self.find_file(filename) if realpath: if self.accepts_gzip() and self.compressible(filename): realpath = self.gzip_file(realpath) self.ctx.res = FileApp(realpath) self.ctx.res.cache_control(public=True, max_age=CACHE_CONTROL.ONE_DAY) CONTENT_DISPOSITION.apply(self.ctx.res.headers, inline=True, filename=filename) return raise HTTPNotFound() raise HTTPPreconditionFailed()
def download_package(context, request): """Download package, or redirect to the download link""" package = request.db.fetch(context.filename) if not package: if request.registry.fallback != "cache": return HTTPNotFound() if not request.access.can_update_cache(): return request.forbid() # If we are caching pypi, download the package from pypi and save it releases = request.locator.get_releases(context.name) dist = None for release in releases: if posixpath.basename(release["url"]) == context.filename: dist = release break if dist is None: return HTTPNotFound() LOG.info("Caching %s from %s", context.filename, request.fallback_simple) package, data = fetch_dist( request, dist["url"], dist["name"], dist["version"], dist["summary"], dist["requires_python"], ) disp = CONTENT_DISPOSITION.tuples(filename=package.filename) request.response.headers.update(disp) cache_control = CACHE_CONTROL.tuples( public=True, max_age=request.registry.package_max_age ) request.response.headers.update(cache_control) request.response.body = data request.response.content_type = "application/octet-stream" return request.response if request.registry.stream_files: with request.db.storage.open(package) as data: request.response.body = data.read() disp = CONTENT_DISPOSITION.tuples(filename=package.filename) request.response.headers.update(disp) cache = CACHE_CONTROL.tuples( public=True, max_age=request.registry.package_max_age ) request.response.headers.update(cache) request.response.content_type = "application/octet-stream" return request.response response = request.db.download_response(package) return response
def download_package(context, request): """ Download package, or redirect to the download link """ package = request.db.fetch(context.filename) if not package: if request.registry.fallback not in ['cache', 'mirror']: return HTTPNotFound() if not request.access.can_update_cache(): return request.forbid() # If we are caching pypi, download the package from pypi and save it dists = request.locator.get_project(context.name) dist = None source_url = None for version, url_set in six.iteritems(dists.get('urls', {})): if dist is not None: break for url in url_set: if posixpath.basename(url) == context.filename: source_url = url dist = dists[version] break if dist is None: return HTTPNotFound() LOG.info("Caching %s from %s", context.filename, request.registry.fallback_url) package, data = fetch_dist(request, dist.name, source_url) disp = CONTENT_DISPOSITION.tuples(filename=package.filename) request.response.headers.update(disp) request.response.body = data request.response.content_type = 'application/octet-stream' return request.response response = request.db.download_response(package) return response
def download_package(context, request): """ Download package, or redirect to the download link """ package = request.db.fetch(context.filename) if not package: if request.registry.fallback != "cache": return HTTPNotFound() if not request.access.can_update_cache(): return request.forbid() # If we are caching pypi, download the package from pypi and save it dists = request.locator.get_project(context.name) dist = None source_url = None for version, url_set in six.iteritems(dists.get("urls", {})): if dist is not None: break for url in url_set: if posixpath.basename(url) == context.filename: source_url = url dist = dists[version] break if dist is None: return HTTPNotFound() LOG.info("Caching %s from %s", context.filename, request.fallback_simple) package, data = fetch_dist(request, dist.name, source_url) disp = CONTENT_DISPOSITION.tuples(filename=package.filename) request.response.headers.update(disp) cache_control = CACHE_CONTROL.tuples( public=True, max_age=request.registry.package_max_age ) request.response.headers.update(cache_control) request.response.body = data request.response.content_type = "application/octet-stream" return request.response if request.registry.stream_files: with request.db.storage.open(package) as data: request.response.body = data.read() disp = CONTENT_DISPOSITION.tuples(filename=package.filename) request.response.headers.update(disp) cache = CACHE_CONTROL.tuples( public=True, max_age=request.registry.package_max_age ) request.response.headers.update(cache) request.response.content_type = "application/octect-stream" return request.response response = request.db.download_response(package) return response
def download_package(context, request): """ Download package, or redirect to the download link """ package = request.db.fetch(context.filename) if not package: if request.registry.fallback != "cache": return HTTPNotFound() if not request.access.can_update_cache(): return request.forbid() # If we are caching pypi, download the package from pypi and save it dists = request.locator.get_project(context.name) dist = None source_url = None for version, url_set in six.iteritems(dists.get("urls", {})): if dist is not None: break for url in url_set: if posixpath.basename(url) == context.filename: source_url = url dist = dists[version] break if dist is None: return HTTPNotFound() LOG.info("Caching %s from %s", context.filename, request.fallback_simple) package, data = fetch_dist(request, dist.name, source_url) disp = CONTENT_DISPOSITION.tuples(filename=package.filename) request.response.headers.update(disp) cache_control = CACHE_CONTROL.tuples( public=True, max_age=request.registry.package_max_age) request.response.headers.update(cache_control) request.response.body = data request.response.content_type = "application/octet-stream" return request.response if request.registry.stream_files: with request.db.storage.open(package) as data: request.response.body = data.read() disp = CONTENT_DISPOSITION.tuples(filename=package.filename) request.response.headers.update(disp) cache = CACHE_CONTROL.tuples(public=True, max_age=request.registry.package_max_age) request.response.headers.update(cache) request.response.content_type = "application/octect-stream" return request.response response = request.db.download_response(package) return response
def download_access_control(self): """ Download the ACL data as a gzipped-json file """ data = self.request.access.dump() compressed = six.BytesIO() zipfile = gzip.GzipFile(mode='wb', fileobj=compressed) zipfile.write(json.dumps(data, separators=(',', ':')).encode('utf8')) zipfile.close() compressed.seek(0) disp = CONTENT_DISPOSITION.tuples(filename='acl.json.gz') self.request.response.headers.update(disp) self.request.response.app_iter = FileIter(compressed) return self.request.response
def download_access_control(self): """ Download the ACL data as a gzipped-json file """ data = self.request.access.dump() compressed = six.BytesIO() zipfile = gzip.GzipFile(mode="wb", fileobj=compressed) zipfile.write(json.dumps(data, separators=(",", ":")).encode("utf8")) zipfile.close() compressed.seek(0) disp = CONTENT_DISPOSITION.tuples(filename="acl.json.gz") self.request.response.headers.update(disp) self.request.response.app_iter = FileIter(compressed) return self.request.response
def download_access_control(self): """ Download the ACL data as a gzipped-json file """ data = self.request.access.dump() compressed = BytesIO() zipfile = gzip.GzipFile(mode='wb', fileobj=compressed) json.dump(data, zipfile, separators=(',', ':')) zipfile.close() compressed.seek(0) disp = CONTENT_DISPOSITION.tuples(filename='acl.json.gz') self.request.response.headers.update(disp) self.request.response.app_iter = FileIter(compressed) return self.request.response
def index_view(request, url=None): """ Root view '/' """ if url: filename, ext = posixpath.splitext(posixpath.basename(url)) ext = ext.lower() filename = filename + "_sprite" + ext if ext == '.gif': img_format = 'GIF' elif ext == '.png': img_format = 'PNG' else: img_format = None stream = download_gif(url) sprite = convert_gif(stream) data = StringIO() sprite.save(data, format=img_format) data.seek(0) disp = CONTENT_DISPOSITION.tuples(filename=filename) request.response.headers.update(disp) request.response.app_iter = FileIter(data) return request.response else: return {}
def download_package(context, request): """ Download package, or redirect to the download link """ package = request.db.fetch(context.filename) if not package: if request.registry.fallback != 'cache': return HTTPNotFound() if not request.access.can_update_cache(): return request.forbid() # If we are caching pypi, download the package from pypi and save it locator = FilenameScrapingLocator(request.registry.fallback_url) dists = locator.get_project(context.name) dist = dists.get(context.filename) if dist is None: return HTTPNotFound() LOG.info("Caching %s from %s", context.filename, request.registry.fallback_url) package, data = fetch_dist(request, dist) disp = CONTENT_DISPOSITION.tuples(filename=package.filename) request.response.headers.update(disp) request.response.body = data request.response.content_type = 'application/octet-stream' return request.response response = request.db.download_response(package) return response