Commit 7594ae20 authored by Arno Töll's avatar Arno Töll
Browse files

Consolidate crypto functions, move allowed_upload to the (new) filesystem...

Consolidate crypto functions, move allowed_upload to the (new) filesystem module where such tasks are now handled
parent da0bcd6a
...@@ -39,7 +39,6 @@ __license__ = 'MIT' ...@@ -39,7 +39,6 @@ __license__ = 'MIT'
import os import os
import logging import logging
import subprocess import subprocess
import md5
import base64 import base64
try: try:
...@@ -48,7 +47,7 @@ except ImportError: # for sqlalchemy 0.7.1 and above ...@@ -48,7 +47,7 @@ except ImportError: # for sqlalchemy 0.7.1 and above
from sqlalchemy.exc import InvalidRequestError from sqlalchemy.exc import InvalidRequestError
from debexpo.lib.base import * from debexpo.lib.base import *
from debexpo.lib.utils import allowed_upload from debexpo.lib.filesystem import CheckFiles
from debexpo.model import meta from debexpo.model import meta
from debexpo.model.user_upload_key import UserUploadKey from debexpo.model.user_upload_key import UserUploadKey
...@@ -105,7 +104,7 @@ class UploadController(BaseController): ...@@ -105,7 +104,7 @@ class UploadController(BaseController):
# Check whether the file extension is supported by debexpo # Check whether the file extension is supported by debexpo
if not allowed_upload(filename): if not CheckFiles().allowed_upload(filename):
log.error('File type not supported: %s' % filename) log.error('File type not supported: %s' % filename)
abort(403, 'The uploaded file type is not supported') abort(403, 'The uploaded file type is not supported')
......
...@@ -164,3 +164,23 @@ class CheckFiles(object): ...@@ -164,3 +164,23 @@ class CheckFiles(object):
if os.path.isdir(path): if os.path.isdir(path):
log.debug("Remove empty package repository '%s'" % (path)) log.debug("Remove empty package repository '%s'" % (path))
os.rmdir(path) os.rmdir(path)
def allowed_upload(self, filename):
"""
Looks at a filename's extension and decides whether to accept it.
We only want package files to be uploaded, after all.
It returns a boolean of whether to accept the file or not.
``filename``
File to test.
"""
for suffix in ['.changes', '.dsc', '.tar.gz', '.diff.gz', '.deb', '.udeb', '.tar.bz2', ".tar.xz"]:
if filename.endswith(suffix):
return True
return False
...@@ -37,28 +37,12 @@ __license__ = 'MIT' ...@@ -37,28 +37,12 @@ __license__ = 'MIT'
import logging import logging
import hashlib import hashlib
import md5
import os import os
from pylons import config from pylons import config
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def allowed_upload(filename):
"""
Looks at a filename's extension and decides whether to accept it.
We only want package files to be uploaded, after all.
It returns a boolean of whether to accept the file or not.
``filename``
File to test.
"""
for suffix in ['.changes', '.dsc', '.tar.gz', '.diff.gz', '.deb', '.udeb', '.tar.bz2', ".tar.xz"]:
if filename.endswith(suffix):
return True
return False
def parse_section(section): def parse_section(section):
""" """
Works out the component and section from the "Section" field. Works out the component and section from the "Section" field.
...@@ -101,7 +85,7 @@ def md5sum(filename): ...@@ -101,7 +85,7 @@ def md5sum(filename):
except: except:
raise AttributeError('Failed to open file %s.' % filename) raise AttributeError('Failed to open file %s.' % filename)
sum = md5.new() sum = hashlib.md5()
while True: while True:
chunk = f.read(10240) chunk = f.read(10240)
if not chunk: if not chunk:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment