def _request(self, token, method, url, data={}, headers={}, **kw): parameters = data_keys(data) parameters.update(kw) request = (oauth.Request .from_consumer_and_token(self.get_consumer(), token, method, url, parameters)) request.sign_request(self.signature_method, self.get_consumer(), token) client = httplib2.Http() if data and method in ['POST', 'PUT']: data = encode_multipart(boundary, data) headers.update({'Content-Type': 'multipart/form-data; boundary=%s' % boundary}) else: data = urllib.urlencode(data) return client.request(request.to_url(), method=method, headers=headers, body=data)
def upload_file(self, filename, dir_id, cover, upload_name=None, maxsize=10, callback=None, dir_=None, encrypt=False, encrypt_func=None): ''' Upload file. :param filename: the absolute path of a file :param dir_id: the id of the folder where the file upload to :param cover: bool viriable, True if you want to cover the file which exists :param upload_name(optional): the name of file when uploaded to vdisk, blank as local name :param maxsize(optional): the max size, 10M as default :param callback(optional): the redirect url, msg will be transfered if set :param dir_(optional): the dir of file exsits, the param dir_id will be ignored if this is set. :return: The example { "fid":"168593", "name":"MIME.txt", "uid":"62", "dir_id":0, "do_dir":"0,82914,82915,82916,82917", "ctime":1288781102, "ltime":1288781102, "size":40049, "type":"text/plain", "md5":"07c2e4630203b0425546091d044d608b", "url":"http://openapi.vdisk.me/open_file/……" } ''' try: if os.path.getsize(filename) > maxsize * (1024 ** 2): raise VdiskError(-2, 'The file is larger than %dM' % maxsize) except os.error: raise VdiskError(-1, 'Can\'t access the file') fp = open(filename, 'rb') try: params = { 'token': self.token, 'dir_id': dir_id, 'cover': 'yes' if cover else 'no', 'file': fp, 'dologid': self.dologid } if upload_name: params['file'] = (fp, upload_name) if callback: params['callback'] = callback if dir_: params['dir'] = dir_ if encrypt and encrypt_func is not None: params, boundary = encode_multipart(params, True, encrypt_func) else: params, boundary = encode_multipart(params) headers = { 'Content-Type': 'multipart/form-data; boundary=%s' % boundary } return self._base_oper('m=file&a=upload_file', params, headers=headers) finally: fp.close()
def upload_file(self, filename, dir_id, cover, upload_name=None, maxsize=10, callback=None, dir_=None, encrypt=False, encrypt_func=None): ''' Upload file. :param filename: the absolute path of a file :param dir_id: the id of the folder where the file upload to :param cover: bool viriable, True if you want to cover the file which exists :param upload_name(optional): the name of file when uploaded to vdisk, blank as local name :param maxsize(optional): the max size, 10M as default :param callback(optional): the redirect url, msg will be transfered if set :param dir_(optional): the dir of file exsits, the param dir_id will be ignored if this is set. :return: The example { "fid":"168593", "name":"MIME.txt", "uid":"62", "dir_id":0, "do_dir":"0,82914,82915,82916,82917", "ctime":1288781102, "ltime":1288781102, "size":40049, "type":"text/plain", "md5":"07c2e4630203b0425546091d044d608b", "url":"http://openapi.vdisk.me/open_file/……" } ''' try: if os.path.getsize(filename) > maxsize * (1024**2): raise VdiskError(-2, 'The file is larger than %dM' % maxsize) except os.error: raise VdiskError(-1, 'Can\'t access the file') fp = open(filename, 'rb') try: params = { 'token': self.token, 'dir_id': dir_id, 'cover': 'yes' if cover else 'no', 'file': fp, 'dologid': self.dologid } if upload_name: params['file'] = (fp, upload_name) if callback: params['callback'] = callback if dir_: params['dir'] = dir_ if encrypt and encrypt_func is not None: params, boundary = encode_multipart(params, True, encrypt_func) else: params, boundary = encode_multipart(params) headers = { 'Content-Type': 'multipart/form-data; boundary=%s' % boundary } return self._base_oper('m=file&a=upload_file', params, headers=headers) finally: fp.close()