def delete_kit_file(kitID, filename): kit = Kit.query.filter_by(id=kitID).first() if not kit: flash('That page does not exist.', 'danger') return redirect(url_for('kits')) if filename not in [file.filename for file in kit.files]: flash('That page does not exist.', 'danger') return redirect(url_for('kits')) if current_user.id != kit.owner.id: flash('You aren\'t allowed to do that.', 'danger') return redirect(url_for('kits')) for file in kit.files: if file.filename == filename: db.session.delete(file) break db.session.commit() s3.Bucket(app.config['AWS_S3_BUCKET_NAME']).delete_objects( Delete={ 'Objects': [ { 'Key': '/'.join(['user_kits', str(kitID), filename]) } ] } ) flash('File deleted.', 'warning') return redirect(url_for('edit_kit', kitID=kitID))
def save_kit_file(kitID, file): filename = secure_filename(file.filename) object_key = '/'.join(['user_kits', str(kitID), filename]) s3.Bucket(app.config['AWS_S3_BUCKET_NAME']).\ upload_fileobj(file, object_key, ExtraArgs={ 'ContentType': file.mimetype }) return filename
def delete_kit(kitID): kit = Kit.query.filter_by(id=kitID).first() if current_user.id != kit.owner.id: flash('You\'re not allowed to do that.') if request.referrer: return redirect(request.referrer) return redirect(url_for('kits')) if not kit: return redirect(url_for('index')) for tag in kit.tags: db.session.delete(tag) db.session.delete(kit) db.session.commit() s3.Bucket(app.config['AWS_S3_BUCKET_NAME']).delete_objects( Delete={ 'Objects': [ { 'Key': '/'.join(['user_kits', str(kitID)]) } ] + [ { 'Key': '/'.join(['user_kits', str(kitID), file.filename]) } for file in kit.files ] } ) flash('Kit deleted successfully', 'success') if request.referrer: return redirect(request.referrer) return redirect(url_for('kits'))
def rename_kit_file(kitID, old_filename, new_filename): new_filename = secure_filename(new_filename) # if new filename has no extension, assume the extension from the # previous filename if not os.path.splitext(new_filename)[1]: old_ext = os.path.splitext(old_filename)[1] new_filename = ''.join([new_filename, old_ext]) old_object_key = '/'.join(['user_kits', str(kitID), old_filename]) new_object_key = '/'.join(['user_kits', str(kitID), new_filename]) # copy object from old path to new path copy_source = { 'Bucket': app.config['AWS_S3_BUCKET_NAME'], 'Key': old_object_key } s3.Bucket(app.config['AWS_S3_BUCKET_NAME']).copy( copy_source, new_object_key) # delete the old object s3.Object(app.config['AWS_S3_BUCKET_NAME'], old_object_key).delete() return new_filename
def save_profile_picture(picture_file): if current_user.pfp_file == 'default.jpg': hexed_filename = secrets.token_hex(13) extension = os.path.splitext(picture_file.filename)[1] new_filename = hexed_filename + extension current_user.pfp_file = new_filename object_key = '/'.join(['images', 'profile_pictures', new_filename]) else: object_key = '/'.join(['images', 'profile_pictures', current_user.pfp_file]) resized_image = Image.open(picture_file).resize(MAX_PFP_SIZE) with io.BytesIO() as final_picture: extension = os.path.splitext(picture_file.filename)[1][1::] resized_image.save(final_picture, format=extension) final_picture.seek(0) s3.Bucket(app.config['AWS_S3_BUCKET_NAME']).\ upload_fileobj(final_picture, object_key, ExtraArgs={ 'ContentType': picture_file.mimetype })