def download(self, destination_path, params=None): """ Downloads a file to the given local path and returns the size of the downloaded file if successful *returns* [Integer] ```python from filestack import Client client = Client('API_KEY', security=sec) filelink = client.upload(filepath='/path/to/file') # if successful, returns size of downloaded file in bytes response = filelink.download('path/to/file') ``` """ if params: CONTENT_DOWNLOAD_SCHEMA.check(params) with open(destination_path, 'wb') as new_file: response = utils.make_call( CDN_URL, 'get', handle=self.handle, params=params, security=self.security, transform_url=(self.url if isinstance( self, filestack.models.Transform) else None)) if response.ok: for chunk in response.iter_content(1024): if not chunk: break new_file.write(chunk) return response
def delete(self, params=None): """ You may delete any file you have uploaded, either through a Filelink returned from the client or one you have initialized yourself. This returns a response of success or failure. This action requires security.abs *returns* [requests.response] ```python from filestack import Client, security # a policy requires at least an expiry policy = {'expiry': 56589012} sec = security(policy, 'APP_SECRET') client = Client('API_KEY', security=sec) filelink = client.upload(filepath='/path/to/file/foo.txt') response = filelink.delete() ``` """ if params: params['key'] = self.apikey else: params = {'key': self.apikey} return utils.make_call(API_URL, 'delete', path=FILE_PATH, handle=self.handle, params=params, security=self.security, transform_url=self.url if isinstance( self, filestack.models.Transform) else None)
def get_content(self, params=None): """ Returns the raw byte content of a given Filelink *returns* [Bytes] ```python from filestack import Client client = Client('API_KEY') filelink = client.upload(filepath='/path/to/file/foo.jpg') byte_content = filelink.get_content() ``` """ if params: CONTENT_DOWNLOAD_SCHEMA.check(params) response = utils.make_call( CDN_URL, 'get', handle=self.handle, params=params, security=self.security, transform_url=(self.url if isinstance( self, filestack.models.Transform) else None)) return response.content
def zip(self, destination_path, files): """ Takes array of files and downloads a compressed ZIP archive to provided path *returns* [requests.response] ```python from filestack import Client client = Client("<API_KEY>") client.zip('/path/to/file/destination', ['files']) ``` """ zip_url = "{}/{}/zip/{}".format(CDN_URL, self.apikey, files) with open(destination_path, 'wb') as new_file: response = utils.make_call(zip_url, 'get') if response.ok: for chunk in response.iter_content(1024): if not chunk: break new_file.write(chunk) return response return response.text
def store(self, filename=None, location=None, path=None, container=None, region=None, access=None, base64decode=None): """ Uploads and stores the current transformation as a Fileink *returns* [Filestack.Filelink] ```python filelink = transform.store() ``` """ if path: path = '"{}"'.format(path) filelink_obj = self.add_transform_task('store', locals()) response = utils.make_call(filelink_obj.url, 'get') if response.ok: data = json.loads(response.text) handle = re.match( r'(?:https:\/\/cdn\.filestackcontent\.com\/)(\w+)', data['url']).group(1) return filestack.models.Filelink(handle, apikey=self.apikey, security=self.security) else: raise Exception(response.text)
def to_filelink(self): """ Checks is the status of the conversion is complete and, if so, converts to a Filelink *returns* [Filestack.Filelink] ```python filelink = av_convert.to_filelink() ``` """ if self.status != 'completed': return 'Audio/video conversion not complete!' response = utils.make_call(self.url, 'get') if response.ok: response = response.json() handle = re.match( r'(?:https:\/\/cdn\.filestackcontent\.com\/)(\w+)', response['data']['url']).group(1) return filestack.models.Filelink(handle, apikey=self.apikey, security=self.security) raise Exception(response.text)
def debug(self): """ Returns a JSON object with inforamtion regarding the current transformation *returns* [Dict] """ debug_instance = self.add_transform_task('debug', locals()) response = utils.make_call(debug_instance.url, 'get') return response.json()
def status(self): """ Returns the status of the AV conversion (makes a GET request) *returns* [String] ```python av_convert= filelink.av_convert(width=100, height=100) while av_convert.status != 'completed': print(av_convert.status) ``` """ response = utils.make_call(self.url, 'get') return response.json()['status']
def zip(self, store=False, store_params=None): """ Returns a zip file of the current transformation. This is different from the zip function that lives on the Filestack Client *returns* [Filestack.Transform] """ params = locals() params.pop('store') params.pop('store_params') new_transform = self.add_transform_task('zip', params) if store: return new_transform.store(**store_params) if store_params else new_transform.store() return utils.make_call(CDN_URL, 'get', transform_url=new_transform.url)
def av_convert(self, preset=None, force=None, title=None, extname=None, filename=None, width=None, height=None, upscale=None, aspect_mode=None, two_pass=None, video_bitrate=None, fps=None, keyframe_interval=None, location=None, watermark_url=None, watermark_top=None, watermark_bottom=None, watermark_right=None, watermark_left=None, watermark_width=None, watermark_height=None, path=None, access=None, container=None, audio_bitrate=None, audio_sample_rate=None, audio_channels=None, clip_length=None, clip_offset=None): """ ```python from filestack import Client client = Client("<API_KEY>") filelink = client.upload(filepath='path/to/file/doom.mp4') av_convert= filelink.av_convert(width=100, height=100) while av_convert.status != 'completed': print(av_convert.status) filelink = av_convert.to_filelink() print(filelink.url) ``` """ new_transform = self.add_transform_task('video_convert', locals()) transform_url = utils.get_transform_url( new_transform._transformation_tasks, external_url=new_transform.external_url, handle=new_transform.handle, security=new_transform.security, apikey=new_transform.apikey, video=True ) response = utils.make_call(transform_url, 'get') if not response.ok: raise Exception(response.text) uuid = response.json()['uuid'] timestamp = response.json()['timestamp'] return filestack.models.AudioVisual( transform_url, uuid, timestamp, apikey=new_transform.apikey, security=new_transform.security )
def get_metadata(self, params=None): """ Metadata provides certain information about a Filehandle, and you can specify which pieces of information you will receive back by passing in optional parameters. ```python from filestack import Client client = Client('API_KEY') filelink = client.upload(filepath='/path/to/file/foo.jpg') metadata = filelink.get_metadata() # or define specific metadata to receive metadata = filelink.get_metadata({'filename': true}) ``` """ metadata_url = "{CDN_URL}/{handle}/metadata".format(CDN_URL=CDN_URL, handle=self.handle) response = utils.make_call(metadata_url, 'get', params=params, security=self.security) return response.json()
def overwrite(self, url=None, filepath=None, params=None): """ You may overwrite any Filelink by supplying a new file. The Filehandle will remain the same. *returns* [requests.response] ```python from filestack import Client, security # a policy requires at least an expiry policy = {'expiry': 56589012} sec = security(policy, 'APP_SECRET') client = Client('API_KEY', security=sec) ``` """ if params: OVERWRITE_SCHEMA.check(params) data, files = None, None if url: data = {'url': url} elif filepath: filename = os.path.basename(filepath) mimetype = mimetypes.guess_type(filepath)[0] files = {'fileUpload': (filename, open(filepath, 'rb'), mimetype)} else: raise ValueError("You must include a url or filepath parameter") return utils.make_call(API_URL, 'post', path=FILE_PATH, params=params, handle=self.handle, data=data, files=files, security=self.security, transform_url=self.url if isinstance( self, filestack.models.Transform) else None)
def upload(self, url=None, filepath=None, multipart=True, params=None, upload_processes=None, intelligent=False): """ Uploads a file either through a local filepath or external_url. Uses multipart by default and Intelligent Ingestion by default (if enabled). You can specify the number of multipart processes and pass in parameters. returns [Filestack.Filelink] ```python from filestack import Client client = Client("<API_KEY>") filelink = client.upload(filepath='/path/to/file') # to use different storage: client = FilestackClient.new('API_KEY', storage='dropbox') filelink = client.upload(filepath='/path/to/file', params={'container': 'my-container'}) # to use an external URL: filelink = client.upload(external_url='https://www.example.com') # to disable intelligent ingestion: filelink = client.upload(filepath='/path/to/file', intelligent=False) ``` """ if params: STORE_SCHEMA.check(params) if filepath and url: raise ValueError( "Cannot upload file and external url at the same time") if intelligent and filepath: response = intelligent_ingestion.upload(self.apikey, filepath, self.storage, params=params, security=self.security) elif multipart and filepath: response = upload_utils.multipart_upload( self.apikey, filepath, self.storage, upload_processes=upload_processes, params=params, security=self.security) else: files, data = None, None if url: data = {'url': url} if filepath: filename = os.path.basename(filepath) mimetype = mimetypes.guess_type(filepath)[0] files = { 'fileUpload': (filename, open(filepath, 'rb'), mimetype) } if params: params['key'] = self.apikey else: params = {'key': self.apikey} path = '{path}/{storage}'.format(path=STORE_PATH, storage=self.storage) if self.security: path = "{path}?policy={policy}&signature={signature}".format( path=path, policy=self.security['policy'].decode('utf-8'), signature=self.security['signature']) response = utils.make_call(API_URL, 'post', path=path, params=params, data=data, files=files) if response.ok: response = response.json() handle = re.match( r'(?:https:\/\/cdn\.filestackcontent\.com\/)(\w+)', response['url']).group(1) return filestack.models.Filelink(handle, apikey=self.apikey, security=self.security) else: raise Exception(response.text)