def upload(): if request.method == 'POST': description = request.cookies.get('description') imagepost = ImagePost(author_id=current_user.id, content=description) db.session.add(imagepost) db.session.commit() for key, f in request.files.items(): if key.startswith('file'): filename = random_filename(f.filename) upload_path = os.path.join( current_app.config['APP_UPLOAD_PATH']) + '/' + str( current_user.id) if not os.path.exists(upload_path): os.mkdir(upload_path) f.save(os.path.join(upload_path, filename)) filename_s = resize_image(f, filename, 200) filename_m = resize_image(f, filename, 800) photo = Photo(filename=filename, filename_s=filename_s, filename_m=filename_m, imagepost_id=imagepost.id, author_id=current_user.id) db.session.add(photo) db.session.commit() attention_body = "您的好友" + current_user.nickname + "发布了图像日志。" attention_url = url_for('main.show_imagepost', id=imagepost.id, _external=True) store_attentions(imagepost.author.get_friends(), attention_body, attention_url) return render_template('upload.html')
def kickoff(): posters = [] poster_notes = [] documents = [] document_notes = [] form = AddProjectForm() if request.method == 'POST': uploadlist_file = request.files.getlist('file') uploadlist_note = request.form.getlist('note') # uploadlist_note will be like ['', ''] when inputs are left blank project_prefix = datetime.now().strftime('%m%d%H%M') for i in range(len(uploadlist_file)): if uploadlist_file[i]: f_name = project_prefix + '_' + random_filename( uploadlist_file[i].filename )[-12:] # this function will remain the original extension, don't bother using os.path if f_name.lower().endswith(('.png', '.jpg', '.jpeg')): posters.append(f_name) poster_notes.append(uploadlist_note[i]) else: documents.append(f_name) if uploadlist_note[i]: document_notes.append(uploadlist_note[i]) else: document_notes.append( os.path.splitext(uploadlist_file[i].filename)[0]) # set default filenote same as the filename uploadlist_file[i].save( os.path.join(app.config['PROJECT_PATH'], f_name)) title_cn = form.title_cn.data title_en = form.title_en.data brief_cn = form.brief_cn.data if form.brief_cn.data else '暂无此项目说明' brief_en = form.brief_en.data if form.brief_en.data else 'No information available' startdate = form.startdate.data enddate = form.enddate.data category = form.category.data banner = posters[0] if posters else "" project = Project(title_cn=title_cn, title_en=title_en, brief_cn=brief_cn, brief_en=brief_en, startdate=startdate, enddate=enddate, filename="*".join(posters), filenote="*".join(poster_notes), banner=banner, category=category) # filename & filenote are misleading but the model of database is set, no way to change it. db.session.add(project) db.session.flush() for i in range(len(documents)): document = Document(filename=documents[i], filenote=document_notes[i], project=project) db.session.add(document) db.session.commit() flash('New project launched!', 'success') return redirect(url_for('.project'))
def update_face(): try: user_data = request.get_json() user_id = int(user_data.get("user_id")) user_face = user_data.get("user_face") file_type = user_data.get("type") user = User.query.filter_by(id=user_id).first() user_avatar = "default.jpg" if user.avatar != "default": user_avatar = "%s.%s" % (user.username, file_type) avatar_filename = random_filename(user_avatar) user.avatar = avatar_filename db.session.commit() avatar_path = '/py3env/West2/West2OlineWork/static/avatars/%s' % avatar_filename Users = [] # 写入图片并保存 with open(avatar_path, 'wb') as w: w.write(base64.b64decode(user_face[23:])) # 获得修改后的头像的base64编码 with open(avatar_path, 'rb') as f: stream = base64.b64encode(f.read()) avatar_base64_str = str(stream, encoding='utf-8') Users.append({ "user_name": user.username, "avatar": avatar_base64_str, "type": file_type, "total_study": user.total_study }) return responseBody(data=Users) except Exception as e: print(e) db.session.commit() return responseError(Responses.PARAMETERS_ERROR)
def upload(): if request.method == 'POST' and 'file' in request.files: f = request.files.get('file') # 获取文件对象 # Dropzone.js 是通过 Ajax 发送请求的, # 每个文件一个请求。为此,无法返回重定向响应, # 使用 flash() 函数或是操作 session。 # 假设我们使用一个 check_image() 函数来检查文件的有效性: # if not check_image(f): # return '无效的图片', 400 # 客户端会把接收到的错误响应显示出来。 filename = random_filename(f.filename) # 生成随机文件名 f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename)) # 保存文件 filename_s = resize_image(f, filename, 400) filename_m = resize_image(f, filename, 800) photo = Photo( # 创建图片记录 filename=filename, filename_s=filename_s, filename_m=filename_m, author=current_user._get_current_object() # 获取真实的用户对象,而不是代理的用户对象 ) db.session.add(photo) db.session.commit() return render_template('main/upload.html')
def upload_imag(username): import os form = AvatarForm() if form.validate_on_submit(): avatar = request.files['avatar'] if avatar.filename != 'default.ico': filename = random_filename(avatar.filename) else: filename = avatar.filename old_filename = current_user.real_avatar UPLOAD_FOLDER = 'C:\\Users\\m1767\\PycharmProjects\\HuLog\\app\\static\\self_imag\\' ALLOWED_EXTENSIONS = ['png', 'ico', 'jpg'] flag = '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS if not flag: flash('该文件格式不支持!') return redirect(url_for('auth.display_user')) avatar.save('{}{}'.format(UPLOAD_FOLDER, filename)) current_user.real_avatar = str(filename) db.session.add(current_user) db.session.commit() flash('更新头像成功') if old_filename != 'default.ico': os.remove(os.path.join(UPLOAD_FOLDER, old_filename)) print('删除成功') return redirect(url_for('auth.display_user')) return render_template('auth/edit_avatar.html', form=form)
def create_sc(): try: data = request.get_json() title = data.get("title") # 接受视频文件 movie_file = request.files.get("file") # 生成随机文件名 filename = random_filename(movie_file.filename) # 生成文件保存路径 save_path = '/%s' % filename # 保存文件 movie_file.save(save_path) # 该视频属于的大章节 fc_id = data.get("fc_id") fc_id = int(fc_id.split("-")[1]) # 构造二级目录 new_sc = SecondCategory(name=title, parent_category_id=fc_id, movie=save_path) db.session.add(new_sc) db.session.commit() return responseSuccess(Responses.OPERATION_SUCCESS) except Exception as e: print(e) db.session.rollback() return responseError(Responses.PARAMETERS_ERROR)
def upload(): if request.method == 'POST': f = request.files.get('file') filename = random_filename(f.filename) f.save(os.path.join(current_app.config['IMAGE_SAVE_PATH'], filename)) url = url_for('main.get_image', filename=filename) return jsonify(location=url)
def update_face(): try: data = request.get_json() user_id=data.get("user_id") user_face=data.get("user_face") user = User.query.filter_by(id=user_id).first() user_avatar = "default.jpg" if user.avatar != "default": user_avatar = user.avatar avatar_filename = random_filename(user_avatar) user.avatar = avatar_filename db.session.commit() try: avatar_path = url_for('static', filename="avatars/%s" % avatar_filename) # 写入图片并保存 with open(avatar_path, 'wb') as w: w.write(base64.b64decode(user_face)) # 获得修改后的头像的base64编码 with open(avatar_path, 'rb') as f: stream = base64.b64encode(f.read()) avatar_base64_str = str(stream, encoding='utf-8') return responseBody(data=[{'avatar': avatar_base64_str}]) except Exception as e: print(e) db.session.rollback() return responseError(Responses.SAVE_FILE_FAIL) except Exception as e: print(e) db.session.rollback() return responseError(Responses.PARAMETERS_ERROR)
def new_note(): form = NoteForm() if form.validate_on_submit(): title = form.title.data category = Category.query.get(form.category.data) body = form.body.data note = Note(title=title, categorys=category, body=body, users=current_user._get_current_object()) #当前用户的真实代理对象 db.session.add(note) db.session.commit() f = form.photo.data filename = random_filename(f.filename) f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename)) filename_x = resize_image(f, filename, 350) photo = Photo( filename=filename, filename_x=filename_x, ) #users=current_user._get_current_object() photo.notes = note db.session.add(photo) db.session.commit() flash('帖子发表成功', 'info') return redirect(url_for('index.index')) return render_template('user/new_note2.html', form=form)
def pubnews(): img_path = [] img_note = [] img_size = [] form = AddNewsForm() if request.method == 'POST': project_prefix = datetime.now().strftime('%C%m%d%H%M') for f in request.files.getlist('file'): if f: f_name = project_prefix + '_' + random_filename( f.filename ) # this function will remain the original extension, don't bother using os.path img_path.append(f_name) f.save(os.path.join(app.config['NEWS_IMG_PATH'], f_name)) image = Image.open( os.path.join(app.config['NEWS_IMG_PATH'], f_name)) img_size.append(str(image.size[0]) + 'x' + str(image.size[1])) sizo = 300 image.thumbnail((sizo, sizo)) image.save( os.path.join(app.config['NEWS_IMG_PATH'], 'thumbnail/' + f_name)) for n in request.form.getlist('note'): img_note.append(n) if img_path: img_jumbo = img_path[0] else: img_jumbo = '' title_cn = form.title_cn.data title_en = form.title_en.data text_cn = form.text_cn.data text_en = form.text_en.data category = form.category.data date = form.date.data location = form.location.data news = News(title_cn=title_cn, title_en=title_en, text_cn=text_cn, text_en=text_en, category=category, date=date, location=location, author=current_user, img_jumbo=img_jumbo, img_path="*".join(img_path), img_note="*".join(img_note), img_size="*".join(img_size)) db.session.add(news) db.session.commit() flash('News released!', 'success') return redirect(url_for('.news'))
def upload(): if request.method == 'POST' and 'file' in request.files: f = request.files.get('file') # 获取图片文件对象 filename = random_filename(f.filename) # 生成随机文件名 f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename)) filename_s = resize_image(f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['small']) filename_m = resize_image(f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['medium']) photo = Photo(filename=filename, filename_s=filename_s, filename_m=filename_m, author=current_app._get_current_object()) db.session.add(photo) db.session.commit() return render_template('main/upload.html')
def upload(): if request.method == 'POST' and 'file' in request.files: f = request.files.get('file') filename = random_filename(f.filename) f.save(os.path.join(current_app.config['APP_UPLOAD_PATH'], filename)) filename_s = resize_image(f, filename, 400) filename_m = resize_image(f, filename, 800) photo = Photo(filename=filename, filename_s=filename_s, filename_m=filename_m, author=current_user._get_current_object()) db.session.add(photo) db.session.commit() return render_template('main/upload.html')
def ups(id): if request.method == 'POST' and 'photo' in request.files: if Photohead.query.filter_by(user_id=id).first(): #修改头像 s = Photohead.query.filter_by(user_id=id).first() sf = Photohead.query.filter_by(user_id=id).first().filename db.session.delete(s) db.session.commit() path = os.path.join( os.path.join(app.config['UPLOADED_PHOTOS_DEST'], sf)) #删除原头像 os.remove(path) ### f = request.files['photo'] ff = random_filename(f.filename) f.filename = ff photos.save(f) #裁剪图片并保存 im = Image.open(app.config['UPLOADED_PHOTOS_DEST'] + '/' + ff) out = im.resize((40, 40)) out.save(app.config['UPLOADED_PHOTOS_DEST'] + '/' + ff) m = Photohead(filename=ff, user_id=current_user.id) db.session.add(m) db.session.commit() return render_template('two/geren.html') #第一次上传头像 f = request.files['photo'] ff = random_filename(f.filename) f.filename = ff photos.save(f) #裁剪图片并保存 im = Image.open(app.config['UPLOADED_PHOTOS_DEST'] + '/' + ff) out = im.resize((40, 40)) out.save(app.config['UPLOADED_PHOTOS_DEST'] + '/' + ff) m = Photohead(filename=ff, user_id=current_user.id) db.session.add(m) db.session.commit() return render_template('two/geren.html') return render_template('two/geren.html')
def upload_photos(album_id): album = Album.query.filter_by(id=album_id).first_or_404() if request.method == 'POST' and 'file' in request.files: f = request.files.get('file') filename = random_filename(f.filename) folder = album.author.username + '/' + album.albumname fname = photosSet.save(f, folder=folder, name=filename) filename_s = resize_image(f, fname, 200) filename_m = resize_image(f, fname, 800) photo = Photo(filename=fname, album=album, filename_s=filename_s, filename_m=filename_m) db.session.add(photo) db.session.commit() return render_template('upload_photos.html', album=album)
def validate_image(fp): try: filename = random_filename(fp.filename) img = Image.open(fp) fp.save( os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename)) except IOError: return None img_m = img.copy() img_s = img.copy() img.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename)) filename_m = resize_image( img_m, filename, current_app.config['ALBUMY_PHOTO_SIZE']['medium']) filename_s = resize_image(img_s, filename, current_app.config['ALBUMY_PHOTO_SIZE']['small']) return dict(name=filename, name_m=filename_m, name_s=filename_s)
def upload(): if request.method == "POST" and "file" in request.files: f = request.files.get("file") filename = random_filename(f.filename) # 随机生成文件名 f.save(os.path.join(current_app.config["ALBUMY_UPLOAD_PATH"], filename)) filename_s = resize_image( f, filename, current_app.config["ALBUMY_PHOTO_SIZE"]["small"]) filename_m = resize_image( f, filename, current_app.config["ALBUMY_PHOTO_SIZE"]["medium"]) photo = Photo(filename=filename, filename_s=filename_s, filename_m=filename_m, author=current_user._get_current_object()) db.session.add(photo) db.session.commit() return render_template("main/upload.html")
def upphoto(): if request.method == 'POST' and 'file' in request.files: f = request.files.get('file') filename = random_filename(f.filename) path = os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename) f.save(path) filename_s = resize_image(filename, path, 400) filename_m = resize_image(filename, path, 800) photo = Photo(filename=filename, filename_s=filename_s, filename_m=filename_m, user_id=1) db.session.add(photo) db.session.commit() return render_template('update.html')
def uploadphoto(): if request.method == 'POST': for f in request.files.getlist('file'): prefix = datetime.now().strftime('%C%m%d') # Security stuff link = prefix + '_' + random_filename(f.filename) path = os.path.join(app.config['ALBUM_PATH'], link) f.save(path) image = Image.open(path) size = str(image.size[0]) + 'x' + str(image.size[1]) image.thumbnail((300, 300)) image.save( os.path.join(app.config['ALBUM_PATH'], 'thumbnail/' + link)) photo = Photo(link=link, size=size) db.session.add(photo) db.session.commit() return ('', 204)
def upload(): if request.method == 'POST' and 'file' in request.files: f = request.files.get('file') filename = random_filename(f.filename) f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename)) #调用编写的剪裁函数 filename_s = resize_image(f, filename, 400) filename_m = resize_image(f, filename, 800) #将文件名写入数据库 photo = Photo(filename=filename, filename_s=filename_s, filename_m=filename_m, users=current_user._get_current_object()) db.session.add(photo) db.session.commit() return render_template('photo/photo.html')
def edit_room(room_id): r = Room.query.get_or_404(room_id) if request.args.get( "anony") == '0' else AnonymousRoom.query.get_or_404(room_id) form = EditForm() if form.validate_on_submit(): r.name = form.name.data r.info = form.info.data f = form.face.data if f: if r.face != "default": os.remove(os.path.join(ROOM_FACE_UPLOAD_PATH, r.face)) filename = random_filename(f.filename) r.face = filename f.save(os.path.join(ROOM_FACE_UPLOAD_PATH, filename)) db.session.commit() flash("保存成功!") return redirect_back() form.name.data = r.name form.info.data = r.info return render_template("edit_room.html", form=form, r=r)
def upload(): """ 处理用户上传图片,生成多个尺寸,保存文件到uploads目录,保存文件名到数据库 :return: 仍在本页面 """ if request.method == "POST" and 'file' in request.files: f = request.files.get("file") filename = random_filename(f.filename) f.save(os.path.join(current_app.config['YOLO_UPLOAD_PATH'], filename)) filename_s = resize_image(f, filename, current_app.config["YOLO_PHOTO_SIZE"]["small"]) filename_m = resize_image(f, filename, current_app.config["YOLO_PHOTO_SIZE"]["medium"]) photo = Photo( filename = filename, filename_s = filename_s, filename_m = filename_m, its_author = current_user._get_current_object() ) db.session.add(photo) db.session.commit() return render_template("main/upload.html")
def upload(): if request.method == 'POST' and 'file' in request.files: f = request.files.get('file') # 获取图片对象 filename = random_filename(f.filename) # 重命名文件 f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename)) # 保存图片文件到服务器目录 filename_s = resize_image( f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['small']) filename_m = resize_image( f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['medium']) # 创建图片的数据库记录 photo = Photo( filename=filename, filename_s=filename_s, filename_m=filename_m, # 这里使用author关系属性与用户建立关系,需要对代理对象current_user调用_get_current_object方法获取真实用户对象,而不是使用代理对象current_user author=current_user._get_current_object()) db.session.add(photo) db.session.commit() return render_template('main/upload.html')
def upload(): if request.method == 'POST': f = request.files.get('file') global filename filename = random_filename(f.filename) f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #保存原图 photo = Photo(filename=filename, author_id=current_user.id) db.session.add(photo) db.session.commit() #裁剪图片并保存 im = Image.open(app.config['UPLOAD_FOLDER'] + '/' + filename) out = im.resize((260, 200)) out.save(app.config['SMALLUPLOAD_FOLDER'] + '/' + filename) im = Image.open(app.config['UPLOAD_FOLDER'] + '/' + filename) b = (im.size[0]) / (im.size[1]) print(b) out = im.resize((800, int(800 / b))) out.save(app.config['BIAOZHUNTUPIAN'] + '/' + filename) return redirect(url_for('two.a')) #return render_template('two/upload.html') return render_template('two/shangchuan.html')
def upload(): if request.method == 'POST' and 'file' in request.files: try: f = request.files.get('file') filename = random_filename(f.filename) # save方法应该放在前面,因为resize_image会将文件流读取 f.save(os.path.join(current_app.config['ALBUMY_UPLOAD_PATH'], filename)) # PIL.Image.open方法使用fp.seek(0)可以从头读取文件流 filename_s = resize_image(f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['small']) filename_m = resize_image(f, filename, current_app.config['ALBUMY_PHOTO_SIZE']['medium']) photo = Photo( filename=filename, filename_s=filename_s, filename_m=filename_m, author=current_user._get_current_object() ) db.session.add(photo) db.session.commit() return 'Success', 200 except Exception as e: # print(e) return 'Invalid image or server error', 415 return render_template('main/upload.html')
def save(): data = request.file.get('editormd-image-file') filename = random_filename(f.filemame) ret, info = qiniu_store.save(data, filename) return redirect(url_for('.url'))
def upload_img(): if request.method == 'POST': file = request.files['file'] f_name = random_filename(file.filename) file.save(os.path.join(app.config['NEWS_IMG_PATH'], f_name)) return json.dumps({'filename': f_name})