def upload_binary_file(root, path): abs_path = os.path.join(root, path) basename = os.path.basename(abs_path) base_url = Setting.query.filter_by(name='base_url').first().value namespace = Setting.query.filter_by(name='namespace').first().value response_code = 0 while response_code != 200: response = session.get('http://{}/Special:Upload'.format(base_url)) response_code = response.status_code data = scrape_inputs(response.text) data['wpDestFile'] = '{}_{}'.format(namespace, basename) files = {'wpUploadFile': open(abs_path, 'rb')} response_code = 0 while response_code != 200: response = session.post('http://{}/Special:Upload'.format(base_url), data=data, files=files) response_code = response.status_code # Find the external path m = re.search('"(/wiki/images/.+?)"', response.text) external_path = m.group(1) uploaded_file = UploadedFile.query.filter_by(name=basename) if not uploaded_file: uploaded_file = UploadedFile(name=basename, external_path=external_path) uploaded_file.external_path = external_path db.session.add(uploaded_file)
def index(): if request.method == 'POST': uploaded_file = request.files['file'] w = request.form.get('w') h = request.form.get('h') if not uploaded_file: return abort(400) # Bad Request # TODO: unique constraint failed if w and h: uploaded_file = UploadedFile.resize(uploaded_file, w, h) else: uploaded_file = UploadedFile.create_by_uploaded_file(uploaded_file) uploaded_file.save() return jsonify( { 'url_d': uploaded_file.url_d, 'url_i': uploaded_file.url_i, 'url_s': uploaded_file.url_s, 'url_p': uploaded_file.url_p, 'filename': uploaded_file.filename, 'size': humanize_bytes(uploaded_file.size), 'time': str(uploaded_file.uploaded_time), 'type': uploaded_file.type, 'quoteurl': uploaded_file.quoteurl, } ) return render_template('index.html', **locals())
def add_new_file(self, file_details_list): uploaded_file = UploadedFile(filename=file_details_list[0], data=file_details_list[1], user_id=self.user_id) db.session.add(uploaded_file) db.session.commit() return uploaded_file.id
def resize(img_hash): w = request.args.get('w') h = request.args.get('h') if w and h: old_file = UploadedFile.objects.get_or_404(filehash=img_hash) new_file = UploadedFile.resize(old_file, w, h) return new_file.url_i
def upload(): form = UploadForm() if form.validate_on_submit(): file_data = form.file.data filename = current_user.username + '_' + secure_filename( file_data.filename) file_data.save(os.path.join(app.config['UPLOAD_PATH'], filename)) flash('File uploaded.') file_upload = UploadedFile(filename=filename, user_id=current_user.get_id()) db.session.add(file_upload) db.session.commit() return render_template('upload.html', form=form)
def accept_data(audit_id, filename, checksum=None): audit = Audit.query.filter_by(id=int(audit_id)).first() accepted = request.args.get('accepted', default=0) if accepted: # data accept, push to db with open(os.path.join(app.config['UPLOAD_FOLDER'], filename)) as csv2_file: header = next(csv.reader(csv2_file)) with open(os.path.join(app.config['UPLOAD_FOLDER'], filename)) as csv3_file: reader = csv.DictReader(csv3_file) db_response = submit_data_db(data_dict=reader, data_headers=header, audit_obj=audit) u = UploadedFile(filename_stored=filename, checksum=checksum, audit_id=audit_id) db.session.add(u) db.session.commit() return redirect(url_for('audit_report', audit_id=audit.id)) else: # check for duplicate import file before accepting with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb') as csv_file: hash_sha512 = hashlib.sha512() chunk = csv_file.read() hash_sha512.update(chunk) checksum = str(hash_sha512.hexdigest()) duplicates = db.session.query(UploadedFile).filter_by( checksum=checksum) if duplicates.first(): proceed_url = url_for('accept_data', audit_id=audit.id, filename=filename, accepted=True, checksum=checksum) return render_template('audit/data_warning.html', duplicates=duplicates.all(), audit=audit, proceed_url=proceed_url) else: return redirect( url_for('accept_data', audit_id=audit.id, filename=filename, accepted=True, checksum=checksum))
def upload_file(): # os.listdir('/Volumes') lists drives on MacOS # desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop') # This is the way to properly get to desktop form = UploadFileForm() if form.is_submitted(): # print(form.data) if form.path.data == '': name_tuple = save_file(file=form.uploaded_file.data) file_obj = UploadedFile(path='/static/uploads/', drive='app.root_path', filename=name_tuple[0], uuid_name=name_tuple[1]) db.session.add(file_obj) db.session.commit() flash('File has been uploaded successfully.', 'success') return redirect(url_for('view_files')) # file_object = UploadedFile(path=) return render_template('file_uploads.html', form=form, active='upload')
def newSheetPage(request): """ NewSheet view to show the UploadSheetForm """ # TODO: Create an option to upload a new version of a file student = getStudent(request.user) if request.method == "POST": sheetForm = UploadSheetForm(student=student, data=request.POST) fileForm = UploadFileForm(data=request.POST, files=request.FILES.getlist('file')) if fileForm.is_valid() and sheetForm.is_valid(): files = request.FILES.getlist('file') sheet = sheetForm.save(commit=False) if splitext(files[0].name)[1] in ('.jpeg', '.jpg', '.png'): sheet.thumbnail = files[0] print('THUMBNAILED') else: sheet.thumbnail = None sheet.uploadedBy = student sheet.save() for file in files: uploadedFile = UploadedFile() uploadedFile.file = file uploadedFile.extension = splitext(file.name)[1] uploadedFile.contentType = file.content_type uploadedFile.relatedSheet = sheet uploadedFile.save() localVarsJSON = json.dumps({'success': 'true',}) messages.add_message(request, messages.SUCCESS, 'Votre fiche a bien été envoyée !') return HttpResponse(localVarsJSON, content_type='application/json') else: errors = [] if not fileForm.is_valid(): errors.append(fileForm.errors) if not sheetForm.is_valid(): errors.append(sheetForm.errors) return HttpResponse(json.dumps(errors), content_type='application/json') else: raise Http404('Hey :/ I wasn\'t expecting you here !')
def s(shortlink): uploaded_file = UploadedFile.get_file_by_shortlink(shortlink) return redirect(uploaded_file.url_p)