def user_image_upload(): identity = user_session.identity if 'file' not in request.files: raise BadRequest('Invalid request') f = request.files['file'] # if not (0 < f.content_length < app.config['IMAGE_UPLOAD_MAX_SIZE']): # raise BadRequest('Invalid file size') storage = ImageStorage(f.stream, 'user-image') if not storage.in_format(app.config['IMAGE_UPLOAD_ALLOWED_EXTENSIONS']): raise BadRequest('Invalid image format') original_model = storage.save() big_thumb = storage.copy().fit(132, 132).save_thumbnail('middle') small_thumb = storage.copy().fit(32, 32).save_thumbnail('small') db.session.add(original_model) db.session.commit() profile = Profile.query.filter(Profile.user_id == identity.id).one() profile.image_id = original_model.id db.session.commit() return { 'orig': original_model.src(), 'middle': big_thumb.src(), 'small': small_thumb.src() }
def retrieve_feed_image(src): remote_file = http.retrieve_remote_file(src) try: storage = ImageStorage(remote_file.open(), 'feed-icons') if storage.in_format(app.config['IMAGE_UPLOAD_ALLOWED_EXTENSIONS']): model = storage.save() storage.copy().fit(120, 120).save_thumbnail('package-icon') storage.copy().fit(16, 16).save_thumbnail('small-package-icon') return model finally: remote_file.remove() return None