def upload(): """Request to Upload an image; When a user is logged in, they have the ability to upload an image of there choice. It must be of image file type. If it is not the right file type, they will be presented with the File error display. Otherwise, they will be redirected to the My Images page.""" db = COMP249Db() curr_user = users.session_user(db) if not curr_user: redirect('/') imagefile = request.files.get('imagefile') name, ext = os.path.splitext(imagefile.filename) if ext not in ('.jpeg', '.jpg', '.png', '.gif'): print("extension error") return template('fileerror.html', title="File Error", session=None, name=None) save_path = os.path.join(UPLOAD_DIR, imagefile.filename) imagefile.save(save_path) interface.add_image(db, imagefile.filename, curr_user) redirect('/my')
def action_upload(environ): form = cgi.FieldStorage(environ=environ, fp=environ['wsgi.input']) logged_user = user_from_cookie(db, environ) if logged_user and 'image' in form and form['image'].filename != '': file_data = form['image'].file.read() filename = form['image'].filename # write the content of the uploaded file to a local file target = os.path.join('static/images', filename) f = open(target, 'wb') f.write(file_data) f.close() add_image(db, filename, logged_user)
def uploadImage(db, userNick): """ 文件上传处理模块 :param db: 数据库对象 :param userNick: 用户名 :return: None """ newfile = bottle.request.files.get("imagefile") # save uploaded image to UPLOAD_DIR savePath = os.path.join(UPLOAD_DIR, newfile.filename) newfile.save(savePath) # update database table "images" interface.add_image(db=db, filename=newfile.filename, usernick=userNick)
def test_add_image(self): """Test that add_image updates the database properly""" imagename = 'new.jpg' usernick = 'carol' interface.add_image(self.db, imagename, usernick) images = interface.list_images(self.db, 5) self.assertEqual(imagename, images[0]['filename'], 'wrong image name after add_image') self.assertEqual(usernick, images[0]['user'], 'wrong user in first image') # date should be today's today = datetime.datetime.today().strftime("%Y-%m-%d") date = images[0]['timestamp'] self.assertEqual(date[:10], today)
def test_add_image(self): """Test that add_image updates the database properly""" imagename = 'new.jpg' usernick = 'carol' interface.add_image(self.db, imagename, usernick) images = interface.list_images(self.db, 5) print images self.assertEqual(imagename, images[0]['filename'], 'wrong image name after add_image') self.assertEqual(usernick, images[0]['user'], 'wrong user in first image') # date should be today's date in UTC to match SQLite today = datetime.datetime.utcnow().strftime("%Y-%m-%d") date = images[0]['timestamp'] self.assertEqual(date[:10], today)
def test_add_image(self): """Test that add_image updates the database properly""" imagename = 'new.jpg' useremail = '*****@*****.**' interface.add_image(self.db, imagename, useremail) # check with a raw SQL query cursor = self.db.cursor() cursor.execute("select filename, date from images where useremail=?", (useremail,)) result = cursor.fetchall() self.assertEqual(1, len(result), 'expected one image for carol') self.assertEqual(imagename, result[0][0], 'wrong image name after add_image') # date should be todays today = datetime.datetime.today().strftime("%Y-%m-%d") date = result[0][1] self.assertEqual(today, date)
def upload(environ, start_response): current_user = users.user_from_cookie(db, environ) formdata = cgi.FieldStorage(environ=environ, fp=environ["wsgi.input"]) if "file" in formdata and formdata["file"].filename != "": file_data = formdata["file"].file.read() filename = formdata["file"].filename # write the content of the uploaded file to the static image directory target = os.path.join("static/images", filename) f = open(target, "wb") f.write(file_data) f.close() interface.add_image(db, filename, current_user) start_response("301 Redirect", [("content-type", "text/html")]) # refresh my_page redirect_my_page = """<html><head><meta http-equiv="refresh" content="0; url=/my" /> </head></html>""" return [redirect_my_page.encode()]