diff --git a/nc_http/core/file/__init__.py b/nc_http/core/file/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nc_http/core/file/func.py b/nc_http/core/file/func.py new file mode 100644 index 0000000..b571dcc --- /dev/null +++ b/nc_http/core/file/func.py @@ -0,0 +1,29 @@ +import os +import time + +import requests +from werkzeug.datastructures import FileStorage + + +def download_file(url: str, path: str) -> str: + r = requests.get(url) + with open(path, 'wb') as f: + f.write(r.content) + return path + + +def save_file(f: FileStorage, path: str, namespace: str=None, filename: str=None) -> str: + if namespace: + save_path = os.path.join(path, namespace.replace('.', os.path.sep)) + else: + save_path = path + + if not os.path.exists(save_path): + os.makedirs(save_path) + + filename = filename or '{}{}'.format(str(int(time.time() * 1000)), os.path.splitext(f.name)[-1]) + file_path = os.path.join(save_path, filename) + with open(file_path, 'wb+') as tmp_f: + tmp_f.write(f.read()) + + return os.path.join(save_path, filename) diff --git a/nc_http/tools/helpers/file.py b/nc_http/tools/helpers/file.py index 202215e..e69de29 100644 --- a/nc_http/tools/helpers/file.py +++ b/nc_http/tools/helpers/file.py @@ -1,15 +0,0 @@ -import os -import time - - -def save_file(f, clip=None, root=None): - clip = clip or '' - clip_path = os.path.join(root, clip, str(int(time.time()))) - if not os.path.exists(clip_path): - os.makedirs(clip_path) - - file_path = os.path.join(clip_path, f.filename) - with open(file_path, 'wb+') as tmp_f: - tmp_f.write(f.read()) - - return file_path