def upload(): """ Uploads a file to the site storage on the server """ # todo: add auto-rename # todo: add format checking and size for pictures form = SimpleUploadForm(request.POST) template = env.get_template('upload.html') if request.method == 'POST' and form.validate(): up_file = form.upload_file.data folder = os.path.join(config.ROOT_FOLDER, form.file_folder.data) if not os.path.exists(folder): os.makedirs(folder) new_filename = up_file.filename file_path = os.path.join(folder, new_filename) # Generate unique filename if one already exists if os.path.exists(file_path): new_filename = distort_filename(up_file.filename) file_path = os.path.join(folder, new_filename) # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(up_file.file.read()) uploaded_file = join_all_path(['/', form.file_folder.data, new_filename]).replace('\\', '/') app.flash(u'Файл завантажено') return template.render(form=form, uploaded_file=uploaded_file) return template.render(form=form)
def backup_db(): backup_name = '{db}_backup_{date}.sql'.format( db=config.DB_NAME, date=time.strftime('%d_%m_%Y_%H-%M')) backup_file = join_all_path([config.ROOT_FOLDER, 'uploaded', backup_name]) command = '/usr/local/bin/mysqldump -u{user} -p{password} {db}'.format( user=config.DB_USER, password=config.DB_PASS, db=config.DB_NAME) with open(backup_file, 'w') as output: p = subprocess.Popen(command, shell=True, stdout=output).wait() # todo: remove the file return backup_name
def up_file(): """ Uploads a picture to the article """ up_file = request.files.get('file') web_folder = 'img/article/' pictures_folder = static_path(web_folder) new_filename = unique_filename(up_file.filename) file_path = os.path.join(pictures_folder, new_filename) # todo: check for file existence # photo_file.save('/img/gallery/') # new Bottle with open(file_path, 'wb') as open_file: open_file.write(up_file.file.read()) return join_all_path(['/', web_folder, new_filename])
def backdb(): backup_name = backup_db() backup_file = join_all_path([config.ROOT_FOLDER, 'uploaded', backup_name]) root_f = join_all_path([config.ROOT_FOLDER, 'uploaded']) return static_file(backup_name, root=root_f, download=backup_name)