Example #1
0
def update_expiring_files():
    """Updates file entries that expires in an hour."""
    next_hour = datetime.utcnow() + timedelta(hours=1)

    unistorage = UnistorageClient(settings.UNISTORAGE_URL,
                                  settings.UNISTORAGE_ACCESS_TOKEN)
    for db_file in File.query.filter(File.unistorage_valid_until != None,
                                     File.unistorage_valid_until <= next_hour).all():
        try:
            unistorage_file = unistorage.get_file(db_file.unistorage_resource_uri)

            if isinstance(unistorage_file, UnistoragePendingFile):
                raise UnexpectedFileException()

            if isinstance(unistorage_file, UnistorageRegularFile):
                db_file.unistorage_valid_until = None
            else:
                db_file.unistorage_valid_until = datetime.utcnow() + \
                    timedelta(seconds=unistorage_file.ttl)

            db_file.unistorage_url = unistorage_file.url
            db.session.add(db_file)
            db.session.commit()
        except (UnistorageError, UnistorageTimeout, UnexpectedFileException) as e:
            print str(e)
Example #2
0
def create_file():
    user_file = request.files.get('file')
    if not user_file:
        return redirect(url_for('core.index'))

    unistorage = UnistorageClient(settings.UNISTORAGE_URL,
                                  settings.UNISTORAGE_ACCESS_TOKEN)
    try:
        unistorage_file = unistorage.upload_file(
            user_file.filename, user_file, type_id=current_user.id)

        db_file = File()
        db_file.user = current_user
        db_file.name = unistorage_file.name
        db_file.unistorage_resource_uri = unistorage_file.resource_uri

        if isinstance(unistorage_file, UnistoragePendingFile):
            raise UnexpectedFileException()

        if isinstance(unistorage_file, UnistorageRegularFile):
            db_file.unistorage_valid_until = None
        else:
            db_file.unistorage_valid_until = timedelta(seconds=unistorage_file.ttl)

        db_file.unistorage_url = unistorage_file.url
        db.session.add(db_file)
        db.session.commit()
    except (UnistorageError, UnistorageTimeout, UnexpectedFileException):
        flash(u'Во время загрузки файла произошла ошибка. Попробуйте позже.')

    return redirect(url_for('.index'))