def post(self): """创建文件/目录 若使用nginx处理upload,则文件的创建会交给Handle""" path = self.get_argument('path') if not self.db.has_dir(path): # not caused by user, just raise it raise HTTPError(404, 'target dir not exists') owner = self.current_user['id'] ownername = self.current_user['nickname'] finfo = dict(dir=path, owner=owner, ownername=ownername) filetype = self.get_argument('type') assert filetype in ('dir', 'file') if filetype == 'file': assert options.static_server == 'tornado' files = self.request.files.get('file') for f in files: relpath = utils.make_relpath(self, f.filename, path) fullpath = os.path.join(options.files_path, relpath[1:]) # relpath.lstrip('/')) with open(fullpath, 'wb') as tmp: yield tmp.write(f.body) finfo.update({ 'name': f.filename, 'type': f.content_type, 'relpath': relpath, 'size': len(f.body), 'hash': utils.md5(f.body) }) self.db.save_file(finfo) else: # filetype == 'dir' relpath = utils.make_relpath(self, self.get_argument('name'), path) relpath = relpath.replace('\\', '/') fullpath = os.path.join(options.files_path, relpath.lstrip('/')) os.mkdir(fullpath) finfo.update({ 'name': self.get_argument('name'), 'type': filetype, 'relpath': relpath }) self.db.save_file(finfo) self.write('ok')
def post(self): """创建文件/目录 若使用nginx处理upload,则文件的创建会交给Handle""" path = self.get_argument('path') if not self.db.has_dir(path): # not caused by user, just raise it raise HTTPError(404, 'target dir not exists') owner = self.current_user['id'] ownername = self.current_user['nickname'] finfo = dict(dir=path, owner=owner, ownername=ownername) filetype = self.get_argument('type') assert filetype in ('dir', 'file') if filetype == 'file': assert options.static_server == 'tornado' files = self.request.files.get('file') for f in files: relpath = utils.make_relpath(self, f.filename, path) fullpath = os.path.join(options.files_path, relpath.lstrip('/')) with open(fullpath, 'w') as tmp: tmp.write(f.body) finfo.update({'name': f.filename, 'type': f.content_type, 'relpath': relpath, 'size': len(f.body), 'hash': utils.md5(f.body)}) self.db.save_file(finfo) else: # filetype == 'dir' relpath = utils.make_relpath(self, self.get_argument('name'), path) fullpath = os.path.join(options.files_path, relpath.lstrip('/')) os.mkdir(fullpath) finfo.update({'name': self.get_argument('name'), 'type': filetype, 'relpath': relpath}) self.db.save_file(finfo) self.write('ok')
def post(self): """由nginx upload module转发过来 将上传文件复制到指定文件夹下""" path = self.get_argument('path') if not self.db.has_dir(path): raise HTTPError(404, 'target dir not exists') name = self.get_argument('file.name') relpath = utils.make_relpath(self, name, path) src_path = self.get_argument('file.path') full_path = os.path.join(options.files_path, relpath.lstrip('/')) shutil.copyfile(src_path, full_path) finfo = dict(name=name, dir=path, relpath=relpath, type=self.get_argument('file.content_type'), size=self.get_argument('file.size'), hash=self.get_argument('file.md5'), owner=self.current_user['id'], ownername=self.current_user['nickname']) self.db.save_file(finfo) self.write('ok')
def post(self): """由nginx upload module转发过来 将上传文件复制到指定文件夹下""" path = self.get_argument('path') if not self.db.has_dir(path): raise HTTPError(404, 'target dir not exists') name = self.get_argument('file.name') relpath = utils.make_relpath(self, name, path) src_path = self.get_argument('file.path') full_path = os.path.join(options.files_path, relpath.lstrip('/')) shutil.copyfile(src_path, full_path) finfo = dict(name=name, dir=path, relpath=relpath, type=self.get_argument('file.content_type'), size = self.get_argument('file.size'), hash = self.get_argument('file.md5'), owner = self.current_user['id'], ownername = self.current_user['nickname']) self.db.save_file(finfo) self.write('ok')