You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flyapps/fir_ser/api/utils/storage/localApi.py

57 lines
1.7 KiB

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# project: 3月
# author: liuyu
# date: 2020/3/18
'''
本地存储api
'''
from api.utils.TokenManager import DownloadToken
from fir_ser import settings
import os
import logging
logger = logging.getLogger(__file__)
5 years ago
class LocalStorage(object):
5 years ago
def __init__(self, domain_name, is_https):
self.domain_name = domain_name
self.is_https = is_https
5 years ago
def get_upload_token(self, name, expires):
dtoken = DownloadToken()
5 years ago
return dtoken.make_token(name, expires)
def get_base_url(self):
5 years ago
uri = 'http://'
if self.is_https:
5 years ago
uri = 'https://'
return "%s%s" % (uri, self.domain_name)
def get_download_url(self, name, expires=600, force_new=False):
dtoken = DownloadToken()
download_url = '/'.join([self.get_base_url(), 'download', name])
5 years ago
if settings.DATA_DOWNLOAD_KEY_OPEN:
download_url = "%s?%s=%s" % (
download_url, settings.DATA_DOWNLOAD_KEY, dtoken.make_token(name, expires, force_new=force_new))
return download_url
5 years ago
def del_file(self, name):
file = os.path.join(settings.MEDIA_ROOT, name)
try:
if os.path.isfile(file):
os.remove(file)
return True
except Exception as e:
logger.error("delete file %s failed Exception %s" % (file, e))
return False
def rename_file(self, oldfilename, newfilename):
try:
os.rename(os.path.join(settings.MEDIA_ROOT, oldfilename), os.path.join(settings.MEDIA_ROOT, newfilename))
return True
except Exception as e:
logger.error("rename_file file %s to %s failed Exception %s" % (oldfilename, newfilename, e))
return False