def fetch_image(self, id): dataset = DatasetModel().load(id, user=self.getCurrentUser(), level=AccessType.READ) if 'imageFileId' in dataset: file_model = File() file = file_model.load(dataset['imageFileId'], force=True) return File().download(file, contentDisposition='inline') return None
def removeThumbnails(event): """ When a resource containing thumbnails is about to be deleted, we delete all of the thumbnails that are attached to it. """ thumbs = event.info.get('_thumbnails', ()) fileModel = File() for fileId in thumbs: file = fileModel.load(fileId, force=True) if file: fileModel.remove(file)
def removeThumbnails(event): """ When a resource containing thumbnails is about to be deleted, we delete all of the thumbnails that are attached to it. """ thumbs = event.info.get('_thumbnails', ()) fileModel = File() for fileId in thumbs: file = fileModel.load(fileId, force=True) if file: fileModel.remove(file)
def fetch_reconstruction(self, id, reconstruction, format, contentDisposition): supported_formats = ['emd', 'tiff'] if format not in supported_formats: raise RestException('"%s" is not a supported format.') file_model = File() emd_file = file_model.load(reconstruction['emdFileId'], force=True) if format == 'emd': return File().download(emd_file, contentDisposition=contentDisposition) elif format == 'tiff': self._set_content_disposition('%s.tiff' % emd_file['name'], contentDisposition, 'image/tiff') return self._to_tiff(reconstruction['emdFileId'])
class LabelResource(Resource): def __init__(self): super().__init__() self.resourceName = 'label' self.ann_file_name = "annotation.json" self.coll_m = Collection() self.file_m = File() self.folder_m = Folder() self.item_m = Item() self.upload_m = Upload() self.asset_m = Assetstore() self.setupRoutes() def setupRoutes(self): self.route('GET', (), handler=self.getLabelList) self.route('GET', (':label_id',), self.getLabel) self.route('GET', ('meta',), self.getLabelMeta) self.route('GET', ('create',), self.createLabelFile) self.route('GET', ('by_name',), self.getLabelByName) self.route('POST', (), self.postLabel) # ############# PUBLIC METHODS ################## @access.public @autoDescribeRoute( Description('Get label list').param('assign_id', 'assignment folder id')) @rest.rawResponse @trace def getLabelList(self, assign_id): files = self.folder_m.fileList(assign_id, user=self.getCurrentUser(), data=False, includeMetadata=True, mimeFilter=['application/json']) files = list(files) cherrypy.response.headers["Content-Type"] = "application/json" return dumps(files) @access.public @autoDescribeRoute( Description('Create a new label file inside the label folder if it doesnt exist') .param('assign_id', 'the parent folder id') .param('name', 'image name for which we are creating this label file')) @rest.rawResponse @trace def createLabelFile(self, assign_id, name): p_folder = self.folder_m.load(id=assign_id, user=self.getCurrentUser(), level=AccessType.WRITE) label_folder = find_folder(p_folder=p_folder, name=name, user=self.getCurrentUser(), desc="Label Folder", create=True) file = find_file(p_folder=label_folder, name=self.ann_file_name, user=self.getCurrentUser(), assetstore=self.asset_m.getCurrent(), create=False) if not file: file = create_new_file(p_folder=label_folder, name=self.ann_file_name, user=self.getCurrentUser(), assetstore=self.asset_m.getCurrent()) config_file = self.__find_config(assign_id) if not config_file: printFail("No config file found") return errorMessage("No config file found") else: res = copy_file(src_file=config_file, dest_file=file, user=self.getCurrentUser()) return dumps({ "label_id": res['fileId'] }) return dumps({ "label_id": file['_id'] }) @access.public @autoDescribeRoute( Description('Get labels by file_name') .param('name', 'label file name') .param('assign_id', 'the assignment id')) @rest.rawResponse @trace def getLabelByName(self, name, assign_id): p_folder = self.folder_m.load(assign_id, user=self.getCurrentUser(), level=AccessType.READ) label_folder = find_folder(p_folder=p_folder, name=name, user=self.getCurrentUser(), desc="Label Folder", create=True) # this file is created in <assign_folder>/<label_folder>/assignment.json file = find_file(p_folder=label_folder, name=self.ann_file_name, user=self.getCurrentUser(), assetstore=self.asset_m.getCurrent(), create=False) cherrypy.response.headers["Content-Type"] = "application/json" if file: return self.file_m.download(file) else: return dumps({}) @access.public @autoDescribeRoute( Description('Get label file by id') .param('label_id', 'label file id')) @rest.rawResponse @trace def getLabel(self, label_id): file = self.file_m.load(label_id, level=AccessType.READ, user=self.getCurrentUser()) printOk2(file) cherrypy.response.headers["Content-Type"] = "application/json" return self.file_m.download(file) @access.public @autoDescribeRoute( Description('Get label meta data by id') .param('label_id', 'label file id')) @trace def getLabelMeta(self, label_id): file = self.file_m.load(label_id, level=AccessType.READ, user=self.getCurrentUser()) cherrypy.response.headers["Content-Type"] = "application/json" return dumps(file) @access.public @autoDescribeRoute( Description('Post to label file by id') .param('label_id', 'label file id') .param('labels', 'labels to be updated')) @rest.rawResponse @trace def postLabel(self, label_id, labels): file = self.file_m.load(label_id, level=AccessType.WRITE, user=self.getCurrentUser()) cherrypy.response.headers["Content-Type"] = "application/json" params = {'labels': json.loads(labels)} data = json.dumps(params, indent=2, sort_keys=True) upload = writeData(self.getCurrentUser(), file, data) printOk2(file) printOk(upload) return dumps(upload) # ############# PRIVATE METHODS ################## def __create_new_file(self, folder, file_name): item = self.item_m.createItem(file_name, creator=self.getCurrentUser(), folder=folder, description='label file', reuseExisting=False) file = self.file_m.createFile(size=0, item=item, name=file_name, creator=self.getCurrentUser(), assetstore=self.asset_m.getCurrent(), mimeType="application/json") return file @staticmethod def __get_owner_id(folder): aclList = Folder().getFullAccessList(folder) for acl in aclList['users']: if acl['level'] == AccessType.ADMIN: return str(acl['id']) return None def __get_config_folder(self, label_folder_id): label_folder = Folder().load(label_folder_id, user=self.getCurrentUser(), level=AccessType.READ) ownerId = self.__get_owner_id(label_folder) config_folder = self.folder_m.load(label_folder['meta'][ownerId], level=AccessType.READ, user=self.getCurrentUser()) return config_folder def __find_config(self, folder_id): folder = self.__get_config_folder(folder_id) printOk2("Config folder {}".format(folder)) files = self.folder_m.fileList(folder, self.getCurrentUser(), data=False) for file_path, file in files: printOk(file) if file['name'] == "config.json": return file def __findFolder(self, p_folder, name, desc="", create=False): """ Find folder by name. If not found create the folder :param p_folder: parent folder :param name: name of the folder you want to find inside the parent folder :return: folder doc """ folder = list(self.folder_m.find({'folderId': p_folder['_id'], 'name': name}).limit(1)) if not folder: # check if you are allowed to create, else return nothing if create: folder = self.folder_m.createFolder(parent=p_folder, name=name, creator=self.getCurrentUser(), description=desc, reuseExisting=True) else: return None return folder def __findFile(self, folder, file_name): item = list(self.item_m.find({'folderId': folder['_id'], 'name': file_name}).limit(1)) if not item: return None item = item[0] file = list(self.file_m.find({'itemId': item['_id']}).limit(1)) if not file: return None return file[0]
def createThumbnail(width, height, crop, fileId, attachToType, attachToId): """ Creates the thumbnail. Validation and access control must be done prior to the invocation of this method. """ fileModel = File() file = fileModel.load(fileId, force=True) streamFn = functools.partial(fileModel.download, file, headers=False) event = events.trigger('thumbnails.create', info={ 'file': file, 'width': width, 'height': height, 'crop': crop, 'attachToType': attachToType, 'attachToId': attachToId, 'streamFn': streamFn }) if len(event.responses): resp = event.responses[-1] newFile = resp['file'] if event.defaultPrevented: if resp.get('attach', True): newFile = attachThumbnail(file, newFile, attachToType, attachToId, width, height) return newFile else: file = newFile streamFn = functools.partial(fileModel.download, file, headers=False) if 'assetstoreId' not in file: # TODO we could thumbnail link files if we really wanted. raise Exception('File %s has no assetstore.' % fileId) stream = streamFn() data = b''.join(stream()) image = _getImage(file['mimeType'], file['exts'], data) if not width: width = int(height * image.size[0] / image.size[1]) elif not height: height = int(width * image.size[1] / image.size[0]) elif crop: x1 = y1 = 0 x2, y2 = image.size wr = float(image.size[0]) / width hr = float(image.size[1]) / height if hr > wr: y1 = int(y2 / 2 - height * wr / 2) y2 = int(y2 / 2 + height * wr / 2) else: x1 = int(x2 / 2 - width * hr / 2) x2 = int(x2 / 2 + width * hr / 2) image = image.crop((x1, y1, x2, y2)) image.thumbnail((width, height), Image.ANTIALIAS) out = six.BytesIO() image.convert('RGB').save(out, 'JPEG', quality=85) size = out.tell() out.seek(0) thumbnail = Upload().uploadFromFile(out, size=size, name='_thumb.jpg', parentType=attachToType, parent={'_id': ObjectId(attachToId)}, user=None, mimeType='image/jpeg', attachParent=True) return attachThumbnail(file, thumbnail, attachToType, attachToId, width, height)
def createThumbnail(width, height, crop, fileId, attachToType, attachToId): """ Creates the thumbnail. Validation and access control must be done prior to the invocation of this method. """ fileModel = File() file = fileModel.load(fileId, force=True) streamFn = functools.partial(fileModel.download, file, headers=False) event = events.trigger('thumbnails.create', info={ 'file': file, 'width': width, 'height': height, 'crop': crop, 'attachToType': attachToType, 'attachToId': attachToId, 'streamFn': streamFn }) if len(event.responses): resp = event.responses[-1] newFile = resp['file'] if event.defaultPrevented: if resp.get('attach', True): newFile = attachThumbnail(file, newFile, attachToType, attachToId, width, height) return newFile else: file = newFile streamFn = functools.partial( fileModel.download, file, headers=False) if 'assetstoreId' not in file: # TODO we could thumbnail link files if we really wanted. raise Exception('File %s has no assetstore.' % fileId) stream = streamFn() data = b''.join(stream()) image = _getImage(file['mimeType'], file['exts'], data) if not width: width = int(height * image.size[0] / image.size[1]) elif not height: height = int(width * image.size[1] / image.size[0]) elif crop: x1 = y1 = 0 x2, y2 = image.size wr = float(image.size[0]) / width hr = float(image.size[1]) / height if hr > wr: y1 = int(y2 / 2 - height * wr / 2) y2 = int(y2 / 2 + height * wr / 2) else: x1 = int(x2 / 2 - width * hr / 2) x2 = int(x2 / 2 + width * hr / 2) image = image.crop((x1, y1, x2, y2)) image.thumbnail((width, height), Image.ANTIALIAS) out = six.BytesIO() image.convert('RGB').save(out, 'JPEG', quality=85) size = out.tell() out.seek(0) thumbnail = Upload().uploadFromFile( out, size=size, name='_thumb.jpg', parentType=attachToType, parent={'_id': ObjectId(attachToId)}, user=None, mimeType='image/jpeg', attachParent=True) return attachThumbnail(file, thumbnail, attachToType, attachToId, width, height)
class LabelResource(Resource): def __init__(self): super().__init__() self.resourceName = 'label' self.coll_m = Collection() self.file_m = File() self.folder_m = Folder() self.item_m = Item() self.upload_m = Upload() self.asset_m = Assetstore() self.setupRoutes() def setupRoutes(self): self.route('GET', (), handler=self.getLabelList) self.route('GET', (':label_id',), self.getLabel) self.route('GET', ('meta',), self.getLabelMeta) self.route('GET', ('create',), self.createLabelFile) self.route('GET', ('by_name',), self.getLabelByName) self.route('POST', (), self.postLabel) def createNewFile(self, folder, file_name): item = self.item_m.createItem(file_name, creator=self.getCurrentUser(), folder=folder, description='label file', reuseExisting=False) file = self.file_m.createFile(size=0, item=item, name=file_name, creator=self.getCurrentUser(), assetstore=self.asset_m.getCurrent(), mimeType="application/json") return file def copy(self, srcFile, destFile): upload = self.upload_m.createUploadToFile(destFile, self.getCurrentUser(), srcFile['size']) self.upload_m.handleChunk(upload=upload, chunk=RequestBodyStream(self.file_m.open(srcFile), size=destFile['size']), user=self.getCurrentUser()) return upload @access.public @autoDescribeRoute( Description('Get label list')) @rest.rawResponse def getLabelList(self): printOk('getLabelsList() was called!') try: collection = list(self.coll_m.list(user=self.getCurrentUser(), offset=0, limit=1))[0] files = self.coll_m.fileList(collection, user=self.getCurrentUser(), data=False, includeMetadata=True, mimeFilter=['application/json']) files = list(files) cherrypy.response.headers["Content-Type"] = "application/json" return dumps(files) except: printFail(traceback.print_exc) @staticmethod def getOwnerId(folder): aclList = Folder().getFullAccessList(folder) for acl in aclList['users']: if acl['level'] == AccessType.ADMIN: return str(acl['id']) return None def getConfigFolder(self, label_folder_id): label_folder = Folder().load(label_folder_id, user=self.getCurrentUser(), level=AccessType.READ) ownerId = self.getOwnerId(label_folder) config_folder = self.folder_m.load(label_folder['meta'][ownerId], level=AccessType.READ, user=self.getCurrentUser()) return config_folder def findConfig(self, folder_id): folder = self.getConfigFolder(folder_id) printOk2("Config folder {}".format(folder)) files = self.folder_m.fileList(folder, self.getCurrentUser(), data=False) for file_path, file in files: printOk(file) if file['name'] == "config.json": return file def __findFile(self, folder, file_name): item = list(self.item_m.find({'folderId': folder['_id'], 'name': file_name}).limit(1)) if not item: return None item = item[0] file = list(self.file_m.find({'itemId': item['_id']}).limit(1)) if not file: return None return file[0] @access.public @autoDescribeRoute( Description('Create a new label file if it doesnt exist') .param('file_name', 'label file name').param('folder_id', 'the parent folder id')) @rest.rawResponse def createLabelFile(self, file_name, folder_id): try: folder = self.folder_m.load(folder_id, user=self.getCurrentUser(), level=AccessType.WRITE) file = self.__findFile(folder, file_name) if not file: file = self.createNewFile(folder, file_name) config_file = self.findConfig(folder_id) if not config_file: printFail("No config file found") return errorMessage("No config file found") else: res = self.copy(config_file, file) return dumps({ "label_id": res['fileId'] }) return dumps({ "label_id": file['_id'] }) except: printFail(traceback.print_exc) cherrypy.response.status = 500 @access.public @autoDescribeRoute( Description('Get labels by file_name') .param('file_name', 'label file name').param('folder_id', 'the parent folder id')) @rest.rawResponse def getLabelByName(self, file_name, folder_id): try: folder = self.folder_m.load(folder_id, user=self.getCurrentUser(), level=AccessType.READ) file = self.__findFile(folder, file_name) cherrypy.response.headers["Content-Type"] = "application/json" if file: return self.file_m.download(file) else: return dumps({}) except: printFail(traceback.print_exc) cherrypy.response.status = 500 @access.public @autoDescribeRoute( Description('Get label by id') .param('label_id', 'label file id')) @rest.rawResponse def getLabel(self, label_id): try: file = self.file_m.load(label_id, level=AccessType.READ, user=self.getCurrentUser()) printOk2(file) cherrypy.response.headers["Content-Type"] = "application/json" return self.file_m.download(file) except: # Unknown slug printFail(traceback.print_exc) cherrypy.response.status = 404 @access.public @autoDescribeRoute( Description('Get label by id') .param('label_id', 'label file id')) def getLabelMeta(self, label_id): try: file = self.file_m.load(label_id, level=AccessType.READ, user=self.getCurrentUser()) cherrypy.response.headers["Content-Type"] = "application/json" return dumps(file) except: # Unknown slug printFail(traceback.print_exc) cherrypy.response.status = 404 @access.public @autoDescribeRoute( Description('Post label by id') .param('label_id', 'label file id')) @rest.rawResponse def postLabel(self, label_id, params): try: file = self.file_m.load(label_id, level=AccessType.WRITE, user=self.getCurrentUser()) cherrypy.response.headers["Content-Type"] = "application/json" params['labels'] = json.loads(params['labels']) data = json.dumps(params, indent=2, sort_keys=True) upload = writeData(self.getCurrentUser(), file, data) printOk2(file) printOk(upload) return dumps(upload) except: # Unknown slug printFail(traceback.print_exc) cherrypy.response.status = 404 @access.public @autoDescribeRoute( Description('Post label by id') .param('label_id', 'label file id')) @rest.rawResponse def strokeToOutline(self, strokes): pass