def POST(self): file = xutils.get_argument("file", {}) dirname = xutils.get_argument("dirname") prefix = xutils.get_argument("prefix") name = xutils.get_argument("name") user_name = xauth.current_name() if file.filename != None: filename = file.filename if file.filename == "": return dict(code="fail", message="filename is empty") basename, ext = os.path.splitext(filename) if name == "auto": # filename = str(uuid.uuid1()) + ext filename = generate_filename(None, prefix, ext) # xutils.makedirs(dirname) filepath, webpath = xutils.get_upload_file_path( user_name, filename) # filename = xutils.quote(os.path.basename(x.file.filename)) with open(filepath, "wb") as fout: # fout.write(x.file.file.read()) for chunk in file.file: fout.write(chunk) xmanager.fire("fs.upload", dict(user=user_name, path=filepath)) return dict(code="success", webpath=webpath, link=get_link(filename, webpath))
def POST(self): file = xutils.get_argument("file", {}) type = xutils.get_argument("type", "normal") prefix = xutils.get_argument("prefix", "") filepath = "" filename = "" if not hasattr(file, "filename") or file.filename == "": return xtemplate.render("note/upload_file.html", filepath=filepath, filename=filename) filename = file.filename # Fix IE HMTL5 API拿到了全路径 filename = os.path.basename(filename) filepath, webpath = xutils.get_upload_file_path(filename, prefix=prefix) with open(filepath, "wb") as fout: # fout.write(x.file.file.read()) for chunk in file.file: fout.write(chunk) if type == "html5": return dict(success=True, message="上传成功", link=get_link(filename, webpath)) return xtemplate.render("note/upload_file.html", filepath=webpath, filename=filename, is_img=is_img(filename))
def GET(self): path, webpath = xutils.get_upload_file_path("") show_menu = (xutils.get_argument("show_menu") != "false") path = os.path.abspath(path) return xtemplate.render("fs/fs_upload.html", show_menu = show_menu, path = path)
def POST(self): user_name = xauth.current_name() part_file = True chunksize = 5 * 1024 * 1024 chunk = xutils.get_argument("chunk", 0, type=int) chunks = xutils.get_argument("chunks", 1, type=int) file = xutils.get_argument("file", {}) prefix = xutils.get_argument("prefix", "") dirname = xutils.get_argument("dirname", xconfig.DATA_DIR) dirname = dirname.replace("$DATA", xconfig.DATA_DIR) note_id = xutils.get_argument("note_id") # 不能访问上级目录 if ".." in dirname: return dict(code="fail", message="can not access parent directory") filename = None webpath = "" origin_name = "" if hasattr(file, "filename"): origin_name = file.filename xutils.trace("UploadFile", file.filename) filename = os.path.basename(file.filename) filename = xutils.get_real_path(filename) if dirname == "auto": filename = generate_filename(filename, prefix) filepath, webpath = xutils.get_upload_file_path(user_name, filename, replace_exists=True) dirname = os.path.dirname(filepath) filename = os.path.basename(filepath) else: # TODO check permission. pass if part_file: tmp_name = "%s_%d.part" % (filename, chunk) seek = 0 else: tmp_name = filename seek = chunk * chunksize xutils.makedirs(dirname) tmp_path = os.path.join(dirname, tmp_name) with open(tmp_path, "wb") as fp: fp.seek(seek) if seek != 0: xutils.log("seek to {}", seek) for file_chunk in file.file: fp.write(file_chunk) else: return dict(code="fail", message="require file") if part_file and chunk+1==chunks: self.merge_files(dirname, filename, chunks) try_touch_note(note_id) if note_id != None and note_id != "": xutils.call("note.touch", note_id) return dict(code="success", webpath=webpath, link=get_link(origin_name, webpath))
def test_get_upload_file_path(self): remove_tmp_file("test.txt") path, webpath = get_upload_file_path("user", "test.txt") print() print(path) print(webpath) self.assertEqual(os.path.abspath(config.DATA_PATH + "/files/user/%s/test.txt" % date), path) self.assertEqual("/data/files/user/%s/test.txt" % date, webpath)
def test_get_upload_file_path_1(self): remove_tmp_file("test_1.txt") create_tmp_file("test.txt") path, webpath = get_upload_file_path("test.txt") print() print(path) print(webpath) self.assertEqual(config.DATA_PATH + "/files/%s/test_1.txt" % date, path) self.assertEqual("/data/files/%s/test_1.txt" % date, webpath) remove_tmp_file("test.txt")
def POST(self): part_file = True chunksize = 5 * 1024 * 1024 chunk = xutils.get_argument("chunk", 0, type=int) chunks = xutils.get_argument("chunks", 1, type=int) file = xutils.get_argument("file", {}) prefix = xutils.get_argument("prefix", "") dirname = xutils.get_argument("dirname", xconfig.DATA_DIR) # Fix 安全问题,不能访问上级目录 dirname = dirname.replace("$DATA", xconfig.DATA_DIR) filename = None webpath = "" origin_name = "" if hasattr(file, "filename"): origin_name = file.filename xutils.log("recv {}", file.filename) filename = os.path.basename(file.filename) filename = xutils.quote_unicode(filename) if dirname == "auto": filepath, webpath = xutils.get_upload_file_path( filename, replace_exists=True, prefix=prefix) dirname = os.path.dirname(filepath) filename = os.path.basename(filepath) if part_file: tmp_name = "%s_%d.part" % (filename, chunk) seek = 0 else: tmp_name = filename seek = chunk * chunksize tmp_path = os.path.join(dirname, tmp_name) with open(tmp_path, "wb") as fp: fp.seek(seek) if seek != 0: xutils.log("seek to {}", seek) for file_chunk in file.file: fp.write(file_chunk) else: return dict(code="fail", message="require file") if part_file and chunk + 1 == chunks: self.merge_files(dirname, filename, chunks) return dict(code="success", webpath=webpath, link=get_link(origin_name, webpath))
def POST(self): file = xutils.get_argument("file", {}) dirname = xutils.get_argument("dirname") prefix = xutils.get_argument("prefix") uuidname = xutils.get_argument("uuidname") if file.filename != None: filename = file.filename if file.filename == "": return dict(code="fail", message="filename is empty") basename, ext = os.path.splitext(filename) if uuidname == "true": filename = str(uuid.uuid1()) + ext # xutils.makedirs(dirname) filepath, webpath = xutils.get_upload_file_path(filename, prefix = prefix) # filename = xutils.quote(os.path.basename(x.file.filename)) with open(filepath, "wb") as fout: # fout.write(x.file.file.read()) for chunk in file.file: fout.write(chunk) xmanager.fire("fs.upload", dict(path=filepath)) return dict(code="success", webpath = webpath, link = get_link(filename, webpath))
def GET(self): datapath, webpath = xutils.get_upload_file_path( xauth.current_name(), "") raise web.seeother("/fs/%s" % datapath)
def GET(self): path, webpath = xutils.get_upload_file_path("") return xtemplate.render("fs/fs_upload.html", path=path)