def upload(): if request.method == 'POST': #files = request.files['file'] files = request.files.getlist('file') if files: for file in files: abs_file_path = uploads.path(secure_filename(file.filename)) if not os.path.exists(abs_file_path): uploads.save(file) size = os.path.getsize(abs_file_path) result = Uploadfile(name=secure_filename(file.filename), type=file.content_type, size=size) else: result = Uploadfile(name=secure_filename(file.filename), type=file.content_type, size=0, not_allowed_msg="File already exists!") return simplejson.dumps({"files": [result.get_file()]}) if request.method == 'GET': files = [ f for f in os.listdir(current_app.config['UPLOADED_UPLOADS_DEST']) if os.path.isfile( os.path.join(current_app.config['UPLOADED_UPLOADS_DEST'], f)) ] file_display = [] for f in files: size = os.path.getsize( os.path.join(current_app.config['UPLOADED_UPLOADS_DEST'], f)) file_saved = Uploadfile(name=f, size=size) file_display.append(file_saved.get_file()) return simplejson.dumps({"files": file_display}) return render_template("admin/file_uploader.html", title='File Uploader')
def extract_cover(zip_file, zip_cover_path, file_path): if zip_cover_path: cf = zip_file.read(zip_cover_path) prefix = os.path.splitext(file_path)[0] cover_path = prefix + '.jpg' with open(uploads.path(cover_path), 'wb') as image: image.write(cf) return cover_path else: return ''
def delete_file(filename): if current_user.is_anonymous or current_user.department not in ['admin']: return redirect(url_for('main.index')) #uploads.path -- absolute path file_path = uploads.path(filename) os.remove(file_path) return redirect(url_for('admin.manage_files')) # endregion
def convert_book_format(book_id, convert_tool_path, old_book_format, new_book_format): db_book = Books.query.get(book_id) db_data = Data.query.filter_by(book=book_id, format=old_book_format).first() if db_book is None or db_data is None: return "{format} format not found for this book".format(format=old_book_format) old_file_name = db_data.name + '.' + old_book_format.lower() new_file_name = db_data.name + '.' + new_book_format.lower() old_file_path = uploads.path(old_file_name, db_book.path) new_file_path = uploads.path(new_file_name, db_book.path) if os.path.isfile(old_file_path): if not os.path.isfile(new_file_name): result = helper.convert_book(convert_tool_path, old_file_path, new_file_path) if result is None: new_format = Data(book=db_book.id, name=db_book.data[0].name, format=new_book_format, uncompressed_size=os.path.getsize(new_file_path)) db_book.data.append(new_format) db.session.commit() return result else: return "{format} format already exists".format(format=new_file_name) else: return "{format} not found: {fn}".format(format=old_book_format, fn=db_book.title + "." + old_book_format.lower())
def upload(): for file in request.files.getlist('btn-upload'): if not uploads.extension_allowed(extension(file.filename)): flash( Markup( 'File extension "%s" is not allowed to be uploaded to this server' % extension(file.filename)), "warning") return redirect(url_for('main.index')) # save file file_name, file_extension = os.path.splitext(file.filename) folder = os.path.join(current_user.email, file_name) file_path = helper.resolve_folder_conflict( current_app.config['UPLOAD_FOLDER'], folder) saved_file_name = uploads.save(file, folder=file_path, name=file.filename) # get book meta meta = book_format.process(uploads.path(saved_file_name), file_path, file_name, file_extension[1:]) # move file if meta.title != meta.file_name: new_file_path = os.path.join(current_app.config['UPLOAD_FOLDER'], file_path, meta.title + file_extension) move(uploads.path(saved_file_name), new_file_path) if meta.cover_path: new_cover_path = os.path.join( current_app.config['UPLOAD_FOLDER'], file_path, meta.title + '.jpg') move(meta.cover_path, new_cover_path) # add book Books.add_book(meta) flash(Markup('File upload completed.'), "info") return redirect(url_for('main.index'))
def upload(): """ User may upload a new BOM File """ form = UploadForm() if form.validate_on_submit(): file_ = request.files['file_'] filename = uploads.save(file_) bom = BillOfMaterials.from_kicad_archive(uploads.path(filename)) bom.name = form.name.data bom.version = form.version.data bom.user = g.user db.session.add(bom) db.session.commit() flash("File {} successfully processed.".format(filename), category='success') return redirect(url_for('bom_summary', id=bom.id)) return render_template('upload.html', form=form)
def delete_book(book_id): db_book = Books.query.filter_by(id=book_id).first() if db_book: file_path = uploads.path(db_book.path) # delete readbook ReadBook.query.filter_by(book_id=book_id).delete() # delete author for author in db_book.authors: db_book.authors.remove(author) if len(author.books) == 0: db.session.delete(author) # delete tag for tag in db_book.tags: db_book.tags.remove(tag) if len(tag.books) == 0: db.session.delete(tag) # delete series for series in db_book.series: db_book.series.remove(series) if len(series.books) == 0: db.session.delete(series) # delete language for language in db_book.languages: db_book.languages.remove(language) if len(language.books) == 0: db.session.delete(language) # delete publisher for publisher in db_book.publishers: db_book.publishers.remove(publisher) if len(publisher.books) == 0: db.session.delete(publisher) # delete data for data in db_book.data: db_book.data.remove(data) if data.books is None: db.session.delete(data) # delete comment for comment in db_book.comments: db_book.comments.remove(comment) if comment.books is None: db.session.delete(comment) db.session.delete(db_book) db.session.commit() helper.delete_book(file_path)