-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
87 lines (69 loc) · 2.31 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import webapp2
from google.appengine.ext import ndb
from google.appengine.api import images
import mimetypes
import logging
HTML_POST_PAGE = \
"""
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Image Upload</title>
</head>
<body>
<form action="/file_upload" enctype="multipart/form-data" method="post">
<div><input type="file" name="file"/></div>
<div><input type="submit" value="Upload"></div>
</form>
</body>
</html>
"""
OUTPUT_HTML_PAGE = """
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Image Serve</title>
</head>
<body>
<h1>Serving image dynamically.</h1>
<img src='/dyimg_serve?img_id=%s'></img>
</body>
</html>
"""
#Hardcoded value for Key name
email_id = "[email protected]"
#Updated Image Upload
class MyUser(ndb.Model):
file_name = ndb.StringProperty()
blob = ndb.BlobProperty()
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.write(HTML_POST_PAGE)
#Handler for image upload
class FileUpload(webapp2.RequestHandler):
def post(self):
#Creating entity with fixed id
my_user = MyUser(id=email_id)
#Below lines to be added in the main code to put image in datastore
file_upload = self.request.POST.get("file", None)
file_name = file_upload.filename
my_user.file_name = file_name
#For default version of the image
#my_user.blob = file_upload.file.read()
#For Resized down version of the image
my_user.blob = images.resize(file_upload.file.read(),48,48)
my_user.put()
#Navigating to other page to read image
self.response.out.write(OUTPUT_HTML_PAGE % email_id)
#Handler to read image into the <img> tag in the OUTPUT_HTML_PAGE
class DynamicImageServe(webapp2.RequestHandler):
def get(self):
oldUser = MyUser.get_by_id(self.request.get('img_id'))
if oldUser.blob:
# Updating content type for all
self.response.headers['Content-Type'] = 'image/*'
self.response.out.write(oldUser.blob)
else:
self.response.out.write('No image')
application = webapp2.WSGIApplication([('/',MainPage ),('/file_upload',FileUpload),('/dyimg_serve',DynamicImageServe),], debug=True)