/
home
/
report
/
report
/
utility
/
file_manager
/
File Upload :
llllll
Current File: /home/report/report/utility/file_manager/Drivers.py
import hashlib import os import uuid import jdatetime import requests from decouple import config from .models import * from django.conf import settings class Drivers: class local: @staticmethod def save(file, user=None, from_url=False): file_name = file.name if not from_url else file.split('/')[-1] jDate = jdatetime.datetime.now() fileExt = get_extension(file_name) name = uuid.uuid4().hex + fileExt filePath = str(config('SAVE_PATH')) + "%(year)s/%(month)s/%(day)s/" % {'year': jDate.year, 'month': jDate.month, 'day': jDate.day} if not os.path.exists(filePath): os.makedirs(filePath) if file: destination = open(filePath + name, 'wb+') if not from_url: for chunk in file.chunks(): # c destination.write(chunk) file_size = file.size else: r = requests.get(file) if r.status_code == 200: destination.write(r.content) file_size = destination.tell() else: raise Exception('img url not valid...') consistency_hash = hashlib.sha256(destination.read()).hexdigest() destination.close() if destination: newFile = File.objects.create(name=name, size=file_size, extension=fileExt, user=user, consistency_hash=consistency_hash, url=filePath.replace(Config.FileManager.savePath, ''), original_name=file_name ) newFile.save() return newFile else: return False @staticmethod def delete(file): try: os.remove(settings.__getattr__('MEDIA_ROOT') + '/' + file.url + file.name) file.delete() return True except Exception as e: print(e) return e @staticmethod def update(file): pass def get_extension(file_name): name, extension = os.path.splitext(file_name) return extension
Copyright ©2k19 -
Hexid
|
Tex7ure