def __init__(self, path, tags=[], mimetype=None): self.path = path self.data = {} self.sources = [] self.tags = tags self.hashes = self.get_hashes() self.timestamp = to_iso8601() self.mimetype = mimetype self.status = SAMPLE_STATUS_NEW self.xrefs = {"parent": [], "child": []} # Size boundary check sample_size = os.stat(path).st_size if sample_size > SAMPLE_MAX_FILESIZE: os.unlink(path) raise ValueError( "Sample %s (%s) is bigger than maximum file size allowed: %s" % (path, humansize(sample_size), humansize(SAMPLE_MAX_FILESIZE)) ) if sample_size < SAMPLE_MIN_FILESIZE and self.mimetype != "text/url": os.unlink(path) raise ValueError( "Sample %s (%s) is smaller than minimum file size allowed: %s" % (path, humansize(sample_size), humansize(SAMPLE_MIN_FILESIZE)) ) if not self.check_exists(): self.store_sample() self.prepare_sample() self.store_data()
def validate_submission(form): if not os.access(app.config.get('SAMPLE_SUBMIT_FOLDER'), os.W_OK): flash( gettext('Cannot write to folder: %(folderpath)s', folderpath=app.config.get('SAMPLE_SUBMIT_FOLDER'))) return False if form.sample.name not in request.files: flash(gettext('No file submitted.')) return False fh = request.files[form.sample.name] filename = secure_filename(fh.filename) file_data = fh.read() file_size = len(file_data) # Check size limits if file_size > app.config.get('SAMPLE_MAX_FILESIZE'): flash( gettext( 'Sample %(samplename)s (%(samplesize)s) is bigger than maximum file size allowed: %(maxsize)s', samplename=filename, samplesize=humansize(file_size), maxisze=humansize(app.config.get('SAMPLE_MAX_FILESIZE')))) return False if file_size < app.config.get('SAMPLE_MIN_FILESIZE'): flash( gettext( 'Sample %(samplename)s (%(samplesize)s) is smaller than minimum file size allowed: %(minsize)s', samplename=filename, samplesize=humansize(file_size), minsize=humansize(app.config.get('SAMPLE_MIN_FILESIZE')))) return False file_hash = hashlib.sha256(file_data).hexdigest() # Check if already submitted by this user exists = Submission.query.filter( and_(User.id == current_user.id, Submission.file_hash == file_hash)).first() if exists: flash(gettext('File already submitted.')) return False return True
def __init__(self, path, mimetype=None): self.path = path self.data = {} self.sources = [] self.tags = [] self.hashes = self.get_hashes() self.timestamp = to_iso8601() self.mimetype = mimetype self.status = SAMPLE_STATUS_NEW self.xrefs = { 'parent': [], 'child': [], } # Size boundary check sample_size = os.stat(path).st_size if sample_size > SAMPLE_MAX_FILESIZE: os.unlink(path) raise ValueError( 'Sample %s (%s) is bigger than maximum file size allowed: %s' % (path, humansize(sample_size), humansize(SAMPLE_MAX_FILESIZE))) if sample_size < SAMPLE_MIN_FILESIZE and self.mimetype != "text/url": os.unlink(path) raise ValueError( 'Sample %s (%s) is smaller than minimum file size allowed: %s' % (path, humansize(sample_size), humansize(SAMPLE_MIN_FILESIZE))) if not self.check_exists(): self.store_sample() self.prepare_sample() self.store_data()
def validate_submission(form): if not os.access(app.config.get('SAMPLE_SUBMIT_FOLDER'), os.W_OK): flash(gettext('Cannot write to folder: %(folderpath)s', folderpath=app.config.get('SAMPLE_SUBMIT_FOLDER'))) return False if form.sample.name not in request.files: flash(gettext('No file submitted.')) return False fh = request.files[form.sample.name] filename = secure_filename(fh.filename) file_data = fh.read() file_size = len(file_data) # Check size limits if file_size > app.config.get('SAMPLE_MAX_FILESIZE'): flash(gettext('Sample %(samplename)s (%(samplesize)s) is bigger than maximum file size allowed: %(maxsize)s', samplename=filename, samplesize=humansize(file_size), maxisze=humansize(app.config.get('SAMPLE_MAX_FILESIZE')))) return False if file_size < app.config.get('SAMPLE_MIN_FILESIZE'): flash(gettext('Sample %(samplename)s (%(samplesize)s) is smaller than minimum file size allowed: %(minsize)s', samplename=filename, samplesize=humansize(file_size), minsize=humansize(app.config.get('SAMPLE_MIN_FILESIZE')))) return False file_hash = hashlib.sha256(file_data).hexdigest() # Check if already submitted by this user exists = Submission.query.filter(and_(User.id == current_user.id, Submission.file_hash == file_hash)).first() if exists: flash(gettext('File already submitted.')) return False return True