def ingest_assets(site, config): # for each file encountered in asset directory asset_files = discover_assets(config['asset_source_path']) assets = [] processors = [] for path in asset_files: relative_path = os.path.relpath(path, config['asset_source_path']) m = mimetypes.guess_type(path) if m: mimetype = m[0].split('/')[0] else: raise NotImplementedError(m) # search for metadata mdpath = '{}.metadata'.format(*os.path.splitext(path)) if os.path.exists(mdpath): with open(mdpath, 'rb') as md: document = md.read() header, body = _parse_content_header(document) metadata = _parse_metadata(header, config) else: metadata = {} # compute the file's checksum with open(path,'rb') as f: checksum = util.checksum(f) a = Asset.get(site=site, path=relative_path) if a is None: a = Asset(site=site, path=relative_path) for k, v in metadata.items(): setattr(a, k, v) a.checksum = checksum a.site = site if body: a.body = body assets.append(a) return assets
def process(config, metadata, path, asset=None): relative_path = os.path.relpath(path, config["asset_source_path"]) if asset is None: asset = Asset(path=relative_path) # determine requested previews conf = util.prefixed_keys(config, "asset_") conf.update(util.prefixed_keys(config, "image_")) conf.update(metadata) preview_specs = _parse_preview_specs(conf) fmt = conf["image_preview_format"] # write the image to the output path image = Image.open(path) fname = os.path.splitext(relative_path)[0] if fmt == "JPEG": ext = "jpeg" elif fmt == "PNG": ext = "png" path = os.path.join(conf["asset_build_path"], "{}.{}".format(fname, ext)) dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) copy = image.copy() copy.save(path, format=fmt) asset.width, asset.height = image.size for name, spec in preview_specs.items(): path = os.path.join(conf["asset_build_path"], "{}-{}.{}".format(fname, name, ext)) preview = ImageOps.fit(image, (spec[0], spec[1]), centering=(spec[2], spec[3]), method=Image.ANTIALIAS) preview.save(path, format=fmt) relative_path = os.path.relpath(path, conf["asset_build_path"]) setattr(asset, name, relative_path) for k, v in metadata.items(): if not k.startswith("image_") or k.startswith("asset_"): setattr(asset, k, v) return asset