Skip to content

Commit

Permalink
MIME for md returned
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinaProsche committed Feb 20, 2024
1 parent d09a20a commit a52a9de
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
8 changes: 2 additions & 6 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@
'report': {'doc', 'odt', 'docx', 'md'}
}

# сохраняем те форматы, в которых НЕ НУЖНО проверять MIME:
NOT_MIME_TYPE = {'md'}

DOCUMENT_TYPES = {'Лабораторная работа', 'Курсовая работа', 'ВКР'}
TABLE_COLUMNS = ['Solution', 'User', 'File', 'Criteria', 'Check added', 'LMS date', 'Score']
URL_DOMEN = os.environ.get('URL_DOMEN', f"http://localhost:{os.environ.get('WEB_PORT', 8080)}")
Expand Down Expand Up @@ -175,21 +172,20 @@ def run_task():
file_type = request.form.get('file_type', 'pres')
filename, extension = file.filename.rsplit('.', 1)
file_ext_type = current_user.file_type['type']
check_mime = True if extension not in NOT_MIME_TYPE else False

if not file:
logger.critical("request doesn't include file")
return "request doesn't include file"
if get_file_len(file) * 2 + db_methods.get_storage() > app.config['MAX_SYSTEM_STORAGE']:
logger.critical('Storage overload has occured')
return 'storage_overload'
file_check_response = check_file(file, extension, ALLOWED_EXTENSIONS[file_ext_type], check_mime)
file_check_response = check_file(file, extension, ALLOWED_EXTENSIONS[file_ext_type], check_mime=True)
if file_check_response != "ok":
logger.info('Пользователь загрузил файл с ошибочным расширением: ' + file_check_response)
return file_check_response

if pdf_file:
pdf_file_check_response = check_file(pdf_file, pdf_file.filename.rsplit('.', 1)[1], "pdf", check_mime)
pdf_file_check_response = check_file(pdf_file, pdf_file.filename.rsplit('.', 1)[1], "pdf", check_mime=True)
if pdf_file_check_response != "ok":
logger.info('Пользователь загрузил файл с ошибочным расширением: pdf_' + pdf_file_check_response)
return "pdf_" + pdf_file_check_response
Expand Down
12 changes: 8 additions & 4 deletions app/utils/check_file.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import filetype

def check_file(file, file_extension, allowed_extensions, check_mime):
def check_file(file, file_extension, allowed_extensions, check_mime=True):
if not file_extension in allowed_extensions:
return "not_allowed_extension"

# Проверяем MIME тип (библиотека автоматически умеет переводить MIME в реальное расширение файла).
if check_mime and file_extension != filetype.guess_extension(file):
return "mime_type_does_not_match_extension"
if check_mime:
if file_extension == 'md':
if file.mimetype != 'text/plain':
return "mime_type_does_not_match_extension"
else:
if file_extension != filetype.guess_extension(file):
return "mime_type_does_not_match_extension"

return "ok"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ pdfplumber==0.6.1
pytest~=7.1.2
filetype==1.2.0
language-tool-python==2.7.1
md2pdf
md2pdf==1.0.1
markdown==3.4.4

0 comments on commit a52a9de

Please sign in to comment.