def get(self, asset_id, md={}, **kw): """retrieves a file from the store. In case you use the asset database you can simply give the asset id. Alternatively you can use the filename to directly access the underlying store. You need to give one though otherwise None will be returned. :param asset_id: The internal asset id you want to retrieve from the store :param metadata: optional metadata in case you have stored it yourself. :param **kw: additional metadata :returns: An instance of ``Asset`` which contains all metadata and the filepointer. """ if self.config.assets is not None: try: asset = self.config.assets.get(asset_id) except ObjectNotFound: raise AssetNotFound(asset_id) filename = asset.filename else: md = copy.copy(md) md.update(kw) asset = Asset(_id=asset_id, **md) asset.store = self.config.store asset.uploader = self if not self.config.store.exists(asset_id): raise AssetNotFound(asset_id) return asset
def upload(): body = json.loads(request.data) image_data = body.get("image_data") if not image_data: return failure_response("Missing image") asset = Asset(image_data = image_data) db.session.add(asset) db.session.commit() return success_response(asset.serialize(), 201)
def upload(): body = json.loads(request.data) image_data = body.get("image_data") if image_data is None: return failure_response("No base64 URL to be found") asset = Asset(image_data=image_data) db.session.add(asset) db.session.commit() return success_response(asset.serialize(), 201)
def uploadImage(imageData, imgType, typeId): asset = Asset(image_data=imageData, img_type=imgType, type_id=typeId) if asset is None: return None db.session.add(asset) if imgType == "profile": user = User.query.filter_by(id=typeId).first() user.photo.append(asset) elif imgType == "post": post = Post.query.filter_by(id=typeId).first() post.photos.append(asset) db.session.commit() return asset.serialize()
def upload_picture(user_id): body = json.loads(request.data) username = body.get("username") password = body.get("password") if username is None or password is None: return failure_response("Invalid username or password") was_successful, user = users_dao.verify_credentials(username, password) if not was_successful: return failure_response("Incorrect username or password") image_data = body.get("image_data") if (image_data is None): return failure_response("No base64 image") asset = Asset(image_data=image_data) db.session.add(asset) db.session.commit() return success_response(asset.serialize())
def add( self, fp, filename=None, content_length=None, content_type="application/octet-stream", store_kw={}, metadata={}, asset_id=None, parent=None, variant_name=None, run_processors=True, **kw ): """add a file to the media database :param fp: The file pointer of the file to add, should be seeked to 0 :param filename: The filename to be used for the public. Does not need to correspond to the internal one. :param asset_id: The id (string) of the asset to be used in the asset database. This is the "internal" filename. In case you don't give one, a UUID will be generated. :param content_length: The size of the file to add (optional, will be computed otherwise) :param content_type: The media type of the file to add (optional) :param store_kw: optional parameters to be passed to the store :param parent: if this is an asset derived from an existing asset, this one should be passed in here as parent. This is mostly used by processors to define variants. :param variant_name: an optional name of the variant if this asset is derived from another asset. This can e.g. be "thumb" if this is the thumbnail version of an existing image. Mostly used in combination with ``parent`` :param run_processors: flag to define whether processors should run or not. As we use the same method from within processors these will pass in False to prevent infinite loops. :param **kw: additional parameters stored alongside the file :return: A dictionary containing the final filename, content length and type """ if filename is None: filename = unicode(uuid.uuid4()) if asset_id is None: asset_id = unicode(uuid.uuid4()) # store filepointer via store. we will get some store related data back. # at least filename and content length asset_md = self.config.store.add(fp, asset_id=asset_id, filename=filename) metadata.update(kw) asset = Asset( _id=asset_id, filename=filename, content_type=content_type, content_length=asset_md.content_length, store_metadata=asset_md, variant_name=variant_name, metadata=metadata, ) asset.store = self.config.store asset.uploader = self if self.config.assets is not None: if parent is not None: asset.parent = parent._id asset = self.config.assets.save(asset) # now run any processors defined for this module if run_processors: for p in self.config.processors: p.process(asset, self) return asset
def add(self, fp, filename=None, content_length=None, content_type="application/octet-stream", store_kw={}, metadata={}, asset_id=None, parent=None, variant_name=None, run_processors=True, **kw): """add a file to the media database :param fp: The file pointer of the file to add, should be seeked to 0 :param filename: The filename to be used for the public. Does not need to correspond to the internal one. :param asset_id: The id (string) of the asset to be used in the asset database. This is the "internal" filename. In case you don't give one, a UUID will be generated. :param content_length: The size of the file to add (optional, will be computed otherwise) :param content_type: The media type of the file to add (optional) :param store_kw: optional parameters to be passed to the store :param parent: if this is an asset derived from an existing asset, this one should be passed in here as parent. This is mostly used by processors to define variants. :param variant_name: an optional name of the variant if this asset is derived from another asset. This can e.g. be "thumb" if this is the thumbnail version of an existing image. Mostly used in combination with ``parent`` :param run_processors: flag to define whether processors should run or not. As we use the same method from within processors these will pass in False to prevent infinite loops. :param **kw: additional parameters stored alongside the file :return: A dictionary containing the final filename, content length and type """ if filename is None: filename = unicode(uuid.uuid4()) if asset_id is None: asset_id = unicode(uuid.uuid4()) # store filepointer via store. we will get some store related data back. # at least filename and content length asset_md = self.config.store.add(fp, asset_id=asset_id, filename=filename) metadata.update(kw) asset = Asset( _id=asset_id, filename=filename, content_type=content_type, content_length=asset_md.content_length, store_metadata=asset_md, variant_name=variant_name, metadata=metadata, ) asset.store = self.config.store asset.uploader = self if self.config.assets is not None: if parent is not None: asset.parent = parent._id asset = self.config.assets.save(asset) # now run any processors defined for this module if run_processors: for p in self.config.processors: p.process(asset, self) return asset