Skip to content

Commit dcdab5b

Browse files
committed
changes
1 parent 1d4e949 commit dcdab5b

File tree

4 files changed

+23
-24
lines changed

4 files changed

+23
-24
lines changed

app/api/files.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ def upload_presentation() -> (dict, int):
146146
Config.c.constants.presentation_file_max_size_in_megabytes
147147
)
148148
}, 404
149+
# check if file can be stored (*2.5 is an estimate of pdf and preview)
150+
if not DBManager().check_storage_limit(request.content_length * 2.5):
151+
return {
152+
'message': "Not enough place in database to store file"
153+
}, 404
154+
149155
presentation_file = request.files['presentation']
150156

151157
# check extension and mimetype of file
@@ -172,19 +178,13 @@ def upload_presentation() -> (dict, int):
172178
presentation_file.filename = converted_name
173179
# save nonconverted file with original_name
174180
nonconverted_file_id = DBManager().add_file(non_converted_file, original_name)
175-
if nonconverted_file_id is None:
176-
return {'message': 'Недостаточно места.'}, 404
177181

178182
presentation_file_id = DBManager().add_file(presentation_file, presentation_file.filename)
179-
if presentation_file_id is None:
180-
return {'message': 'Недостаточно места.'}, 400
181183
presentation_file_preview = get_presentation_file_preview(DBManager().get_file(presentation_file_id))
182184
presentation_file_preview_id = DBManager().read_and_add_file(
183185
presentation_file_preview.name,
184186
presentation_file_preview.name,
185187
)
186-
if presentation_file_preview_id is None:
187-
return {'message': 'Недостаточно места.'}, 400
188188
presentation_file_preview.close()
189189
PresentationFilesDBManager().add_presentation_file(
190190
presentation_file_id,

app/api/trainings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ def add_presentation_record(training_id: str) -> (dict, int):
275275
TrainingsDBManager().change_training_status_by_training_id(training_id,
276276
TrainingStatus.SENT_FOR_PREPARATION)
277277
presentation_record_file_id = DBManager().add_file(presentation_record_file)
278-
if presentation_record_file_id is None:
279-
return {'message': 'Недостаточно места.'}, 404
280278
TrainingsDBManager().add_presentation_record(
281279
training_id, presentation_record_file_id, presentation_record_duration,
282280
)

app/mongo_odm.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,26 @@
2929
from app.utils import remove_blank_and_none
3030

3131
logger = get_root_logger()
32-
32+
33+
BYTES_PER_MB = 1024*1024
3334

3435
class DBManager:
35-
def __new__(cls, max_size=20000): # max_size only on first creation
36+
def __new__(cls, max_size=20000): # max_size only on first creation, in MB
3637
if not hasattr(cls, 'init_done'):
3738
cls.instance = super(DBManager, cls).__new__(cls)
3839
connect(Config.c.mongodb.url + Config.c.mongodb.database_name)
3940
cls.instance.storage = GridFSStorage(GridFSBucket(_get_db()))
40-
cls.instance.max_size = max_size
41+
cls.instance.max_size = max_size * BYTES_PER_MB
4142
cls.init_done = True
4243
return cls.instance
4344

44-
# returns id of saved file and None if storage limit exceeded
4545
def add_file(self, file, filename=uuid.uuid4()):
4646
try:
4747
file.seek(0, os.SEEK_END)
4848
size = file.tell()
4949
file.seek(0)
5050
except:
5151
size = len(file)
52-
cur_size = self.check_storage_limit(size)
53-
if cur_size > self.max_size:
54-
logger.error(f"Could not save file {filename}: Storage limit exceeded")
55-
return None # storage limit exceeded, can't add file
5652
_id = self.storage.save(name=filename, content=file)
5753
self.update_storage_size(size)
5854
return _id
@@ -100,17 +96,21 @@ def update_storage_size(self, deltasize):
10096
meta = self._get_or_create_storage_meta()
10197
meta.used_size += deltasize
10298
meta.save()
99+
100+
def get_max_size(self):
101+
return self.max_size
103102

103+
# returns Bool variable - True if file can be stored else False
104104
def check_storage_limit(self, new_file_size):
105105
current_size = self.get_used_storage_size()
106106
inf_msg = (
107107
f"Check for ability to add file: "
108-
f"Current: {current_size/(1024*1024):.2f} MB, "
109-
f"New file: {new_file_size/(1024*1024):.2f} MB, "
110-
f"Storage size: {self.max_size} MB"
108+
f"Current: {current_size/BYTES_PER_MB:.2f} MB, "
109+
f"New file: {new_file_size/BYTES_PER_MB:.2f} MB, "
110+
f"Storage size: {self.max_size/BYTES_PER_MB} MB"
111111
)
112112
logger.info(inf_msg)
113-
return current_size + new_file_size
113+
return False if current_size + new_file_size > self.max_size else True
114114

115115

116116
class TrainingsDBManager:

app/routes/capacity.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from flask import Blueprint, render_template
22
from app.mongo_odm import DBManager
3-
from app.config import Config
43
from app.root_logger import get_root_logger
54
from app.lti_session_passback.auth_checkers import is_admin
65

76
logger = get_root_logger()
87

8+
BYTES_PER_MB = 1024*1024
9+
910
routes_capacity = Blueprint(
1011
'routes_capacity', __name__, url_prefix='/capacity')
1112

@@ -14,12 +15,12 @@ def storage_capacity():
1415
if not is_admin():
1516
return {}, 404
1617
current_size = DBManager().get_used_storage_size()
17-
max_size = int(Config.c.constants.storage_max_size_mbytes)*1024*1024
18+
max_size = DBManager().get_max_size()
1819
ratio = current_size / max_size
1920
return render_template(
2021
'capacity.html',
21-
size=round(current_size / (1024*1024), 2),
22-
max_size=round(max_size / (1024*1024), 2),
22+
size=round(current_size / BYTES_PER_MB, 2),
23+
max_size=round(max_size / BYTES_PER_MB, 2),
2324
ratio=round(ratio * 100, 1)
2425
)
2526

0 commit comments

Comments
 (0)