From 43d61b5348ead40fcc98a26d8804d059583f5d3e Mon Sep 17 00:00:00 2001 From: jiangweidong Date: Fri, 9 Sep 2022 19:05:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=AF=B9=E5=BC=80?= =?UTF-8?q?=E5=90=AFSSL/TLS=E7=9A=84MongoDb=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=94=B9=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/utils/file.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apps/common/utils/file.py b/apps/common/utils/file.py index 9529fbdca..efc652870 100644 --- a/apps/common/utils/file.py +++ b/apps/common/utils/file.py @@ -4,6 +4,10 @@ import csv import pyzipper import requests +from hashlib import md5 + +from django.conf import settings + def create_csv_file(filename, headers, rows, ): with open(filename, 'w', encoding='utf-8-sig')as f: @@ -28,3 +32,18 @@ def download_file(src, path): with open(path, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) + + +def save_content_to_temp_path(content, file_mode=0o400): + if not content: + return + + project_dir = settings.PROJECT_DIR + tmp_dir = os.path.join(project_dir, 'tmp') + filename = '.' + md5(content.encode('utf-8')).hexdigest() + filepath = os.path.join(tmp_dir, filename) + if not os.path.exists(filepath): + with open(filepath, 'w') as f: + f.write(content) + os.chmod(filepath, file_mode) + return filepath