44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
# -*- coding:utf-8 -*-
|
||
'''
|
||
@Author:Robin.lau
|
||
@Email: 329080237@qq.com
|
||
@Wechat: 15618110227
|
||
@File: uploadImg.py
|
||
@Date: 2021/2/27 13:45
|
||
'''
|
||
import re
|
||
import json
|
||
import time
|
||
import random
|
||
import requests
|
||
import hashlib
|
||
from django.conf import settings
|
||
from datetime import datetime
|
||
import os, time, random
|
||
|
||
|
||
def gen_real_file_path():
|
||
auto_path = str(datetime.now().year) + "/" + str(datetime.now().month) + "/" + str(datetime.now().day)
|
||
real_path = os.path.join(settings.UPLOADS, auto_path)
|
||
if not os.path.exists(real_path):
|
||
os.makedirs(real_path)
|
||
filename = time.strftime("%H%M%S", time.localtime(time.time())) + str(random.randint(100, 999)) # 生成时间+3位随机文件名
|
||
filename = hashlib.md5(filename.encode(encoding='UTF-8')).hexdigest()
|
||
real_filepath = real_path + "/" + filename
|
||
url_filepath = settings.UPLOADS_URL + "/" + auto_path + "/" + filename
|
||
# 返回
|
||
return (real_filepath, url_filepath)
|
||
|
||
|
||
|
||
def get_text_content(file_path):
|
||
text_content = ""
|
||
try:
|
||
# 本地文件
|
||
with open(file_path, "r", encoding="utf-8") as file_obj:
|
||
text_content = file_obj.read()
|
||
except Exception as e:
|
||
print(f'--> get_html_text_content 异常:e={e}')
|
||
# 返回
|
||
return text_content
|