def pomsg(): postform = PostForm(request.form) if request.method == 'POST' and postform.validate(): ori_last_mid = database.get_latest_mid() message = { 'mid': str(ori_last_mid + 1), 'time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), 'user': session['logged_in'], 'message': html_escape(postform.message.data) } if request.files[postform.upload.name]: image = request.files[postform.upload.name] pos = image.tell() file_length = len(image.read()) image.seek(pos) if file_length < (2 * 1024 * 1024): if image.content_type.startswith('image/'): filename = session['logged_in'] + ':' + str( ori_last_mid + 1) + ':' + secure_filename( image.filename) s3.s3_put(filename, image) message['image'] = filename image.close() else: current_app.logger.warn( str(session['logged_in']) + ' upload file-content error') flash(flash_config.get('home', 'upload_file_type_error')) image.close() return redirect(url_for('home.home')) else: current_app.logger.warn( str(session['logged_in']) + ' upload file too big') flash(flash_config.get('home', 'upload_file_too_big')) image.close() return redirect(url_for('home.home')) try: database.add_msg(str(ori_last_mid + 1), session['logged_in'], message) except: current_app.logger.error('add message fail') elif request.method == 'POST' and not postform.validate(): form.flash_errors(postform) return redirect(url_for('home.home'))
def pomsg(): postform = PostForm(request.form) if request.method == 'POST' and postform.validate(): ori_last_mid = database.get_latest_mid() message ={ 'mid': str(ori_last_mid+1), 'time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())), 'user': session['logged_in'], 'message': html_escape(postform.message.data) } if request.files[postform.upload.name]: image = request.files[postform.upload.name] pos = image.tell() file_length = len(image.read()) image.seek(pos) if file_length < (2 * 1024 * 1024): if image.content_type.startswith('image/'): filename = session['logged_in'] + ':' + str(ori_last_mid+1) + ':' + secure_filename(image.filename) s3.s3_put(filename, image) message['image'] = filename image.close() else: current_app.logger.warn(str(session['logged_in'])+' upload file-content error') flash(flash_config.get('home', 'upload_file_type_error')) image.close() return redirect(url_for('home.home')) else: current_app.logger.warn(str(session['logged_in'])+' upload file too big') flash(flash_config.get('home', 'upload_file_too_big')) image.close() return redirect(url_for('home.home')) try: database.add_msg(str(ori_last_mid+1), session['logged_in'], message) except: current_app.logger.error('add message fail') elif request.method == 'POST' and not postform.validate(): form.flash_errors(postform) return redirect(url_for('home.home'))
def test001_put_get(self): s3.s3_put(self.image_name, self.image_file) self.image_file.seek(0) uploaded_file = s3.s3_get(self.image_name) assert uploaded_file == base64.encodestring(self.image_file.read())