def mass_attach_project_pictures(projects: typing.Iterable[pillarsdk.Project], *, api, header=True, square=True): """Attach file object to all projects in the list. Queries for all picture files at once and sets the header and square images. """ my_log = log.getChild('mass_attach_project_pictures') if not (header or square): raise ValueError("at least one of header/square must be True") if not projects: return file_ids = set() if header: file_ids.update(p.picture_header for p in projects if p.picture_header) if square: file_ids.update(p.picture_square for p in projects if p.picture_square) if not file_ids: return fid_list = list(file_ids) my_log.debug('mass-fetching %d files %s', len(fid_list), fid_list) file_resp = File.all({'where': {'_id': {'$in': fid_list}}}, api=api) file_obs = {f['_id']: f for f in file_resp['_items']} to_find = len(file_ids) found = 0 missing = 0 for p in projects: if header and p.picture_header: try: p.picture_header = file_obs[p.picture_header] except KeyError: p.picture_header = None my_log.warning('File %r not found, but used as picture_header in project %s', p.picture_header, p['_id']) missing += 1 else: found += 1 if square and p.picture_square: try: p.picture_square = file_obs[p.picture_square] except KeyError: p.picture_square = None my_log.warning('File %s not found, but used as picture_square in project %s', p.picture_square, p['_id']) missing += 1 else: found += 1 my_log.debug('found %d of %d pictures, there were %d missing', found, to_find, missing)
def index(): """Custom files entry point """ rfiles = [] backend = app.config['STORAGE_BACKEND'] api = SystemUtility.attract_api() user = current_user.objectid node_picture = File() for file_ in request.files: filestorage = request.files[file_] # Save file on AttractWeb Storage picture_path = os.path.join( app.config['UPLOAD_DIR'], filestorage.filename) filestorage.save(picture_path) picture_file_file = open(picture_path, 'rb') if backend == 'pillar': hash_ = hashfile(picture_file_file, hashlib.md5()) name = "{0}{1}".format(hash_, os.path.splitext(picture_path)[1]) picture_file_file.close() file_check = node_picture.all( {"where": "path=='{0}'".format(name)}, api=api) file_check = file_check['_items'] if len(file_check) == 0: prop = {} prop['name'] = filestorage.filename prop['description'] = "File {0}".format(filestorage.filename) prop['user'] = user prop['content_type'] = filestorage.content_type # TODO Fix length value prop['length'] = filestorage.content_length prop['md5'] = hash_ prop['filename'] = filestorage.filename prop['backend'] = backend if backend in ["pillar"]: prop['path'] = name node_picture.post(prop, api=api) prop['_id'] = node_picture['_id'] if backend == 'pillar': node_picture.post_file(picture_path, name, api=api) node_picture.build_previews(name, api=api) url = "{0}/file_storage/file/{1}".format( app.config['PILLAR_SERVER_ENDPOINT'], prop['path']) rfiles.append( { "id": prop['_id'], "name": prop['filename'], "size": prop['length'], "url": url, "thumbnailUrl": url, "deleteUrl": url, "deleteType": "DELETE" }) else: url = "{0}/file_storage/file/{1}".format( app.config['PILLAR_SERVER_ENDPOINT'], file_check[0]['path']) rfiles.append( { "id": file_check[0]['_id'], "name": file_check[0]['filename'], "size": file_check[0]['length'], "url": url, "thumbnailUrl": url, "deleteUrl": url, "deleteType": "DELETE" }) return jsonify(dict(files=rfiles))
def index(): """Custom files entry point """ rfiles = [] backend = app.config['STORAGE_BACKEND'] api = SystemUtility.attract_api() user = current_user.objectid node_picture = File() for file_ in request.files: filestorage = request.files[file_] # Save file on AttractWeb Storage picture_path = os.path.join(app.config['UPLOAD_DIR'], filestorage.filename) filestorage.save(picture_path) picture_file_file = open(picture_path, 'rb') if backend == 'pillar': hash_ = hashfile(picture_file_file, hashlib.md5()) name = "{0}{1}".format(hash_, os.path.splitext(picture_path)[1]) picture_file_file.close() file_check = node_picture.all({"where": "path=='{0}'".format(name)}, api=api) file_check = file_check['_items'] if len(file_check) == 0: prop = {} prop['name'] = filestorage.filename prop['description'] = "File {0}".format(filestorage.filename) prop['user'] = user prop['content_type'] = filestorage.content_type # TODO Fix length value prop['length'] = filestorage.content_length prop['md5'] = hash_ prop['filename'] = filestorage.filename prop['backend'] = backend if backend in ["pillar"]: prop['path'] = name node_picture.post(prop, api=api) prop['_id'] = node_picture['_id'] if backend == 'pillar': node_picture.post_file(picture_path, name, api=api) node_picture.build_previews(name, api=api) url = "{0}/file_storage/file/{1}".format( app.config['PILLAR_SERVER_ENDPOINT'], prop['path']) rfiles.append({ "id": prop['_id'], "name": prop['filename'], "size": prop['length'], "url": url, "thumbnailUrl": url, "deleteUrl": url, "deleteType": "DELETE" }) else: url = "{0}/file_storage/file/{1}".format( app.config['PILLAR_SERVER_ENDPOINT'], file_check[0]['path']) rfiles.append({ "id": file_check[0]['_id'], "name": file_check[0]['filename'], "size": file_check[0]['length'], "url": url, "thumbnailUrl": url, "deleteUrl": url, "deleteType": "DELETE" }) return jsonify(dict(files=rfiles))