-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathfilemanager.py
93 lines (76 loc) · 2.99 KB
/
filemanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import unicodedata
import shutil
import logging
import datetime
import re
from werkzeug.utils import secure_filename
from flask import current_app, render_template
from weasyprint import HTML, CSS
from geonature.utils.env import BACKEND_DIR
# get the root logger
log = logging.getLogger()
def remove_file(filepath, absolute_path=False):
try:
if absolute_path:
os.remove(filepath)
else:
os.remove(os.path.join(current_app.config["BASE_DIR"], filepath))
except Exception:
pass
def rename_file(old_chemin, old_title, new_title):
new_chemin = old_chemin.replace(
removeDisallowedFilenameChars(old_title),
removeDisallowedFilenameChars(new_title),
)
os.rename(
os.path.join(current_app.config["BASE_DIR"], old_chemin),
os.path.join(current_app.config["BASE_DIR"], new_chemin),
)
return new_chemin
def upload_file(file, id_media, cd_ref, titre):
filename = ("{cd_ref}_{id_media}_{title}.{ext}").format(
cd_ref=str(cd_ref),
id_media=str(id_media),
title=removeDisallowedFilenameChars(titre),
ext=file.filename.rsplit(".", 1)[1],
)
filepath = os.path.join(current_app.config["UPLOAD_FOLDER"], filename)
file.save(os.path.join(current_app.config["BASE_DIR"], filepath))
return filepath
def removeDisallowedFilenameChars(uncleanString):
cleanedString = secure_filename(uncleanString)
cleanedString = unicodedata.normalize("NFKD", uncleanString)
cleanedString = re.sub("[ ]+", "_", cleanedString)
cleanedString = re.sub("[^0-9a-zA-Z_-]", "", cleanedString)
return cleanedString
def delete_recursively(path_folder, period=1, excluded_files=[]):
"""
Delete all the files and directory inside a directory
which have been create before a certain period
Paramters:
path_folder(string): path to the fomlder to delete
period(integer): in days: delete the file older than this period
exluded_files(list<string>): list of files to not delete
"""
for the_file in os.listdir(path_folder):
file_path = os.path.join(path_folder, the_file)
try:
now = datetime.datetime.now()
creation_date = datetime.datetime.utcfromtimestamp(os.path.getctime(file_path))
is_older_than_period = (now - creation_date).days >= period
if is_older_than_period:
if os.path.isfile(file_path) and not the_file in excluded_files:
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
log.error(e)
def generate_pdf(template, data, filename):
template_rendered = render_template(template, data=data)
html_file = HTML(
string=template_rendered, base_url=current_app.config["API_ENDPOINT"], encoding="utf-8"
)
file_abs_path = str(BACKEND_DIR) + "/static/pdf/" + filename
html_file.write_pdf(file_abs_path)
return file_abs_path