[PYTHON] Let's make a spot sale service 8 (image uploader edition)

I noticed that I was making an item registration and editing function. With the current api, multiple images will be uploaded with one api. The transmission time is very long and the user experience is poor.

I checked Mercari (http://mercari.jp) to see how other apps are doing around here. Mercari seems to upload to the server every time one image is selected.

That's why I tried to imitate it. The uploader is complete. I thought I was touching Mercari, but someday I would like to add an image processing function as well!


#Image upload
class PutImage(UserLoginAuthRequestHandler):

    def post(self):
        if self.invalidateAccount():
            Common.writeUserResponseError(self, 401,u"Invalid session")
            return

        blob    =   self.request.get('blob')
        content_type = self.request.params['blob'].type

        blobHash = hashlib.md5(blob).hexdigest()
        gcsPath = Common.createImagePath(blobHash)
        Common.saveToGCS(gcsPath,blob,content_type)


        entity = ImageFile()
        entity.path = gcsPath
        entity.md5  = blobHash

        entity.put()

        res = dict(
            url = webapp2.uri_for('GetImage',_full=True,imageid=entity.key.id())
        )

        Common.writeUserResponseSuccess(self, res )


#Returns the specified image
class GetImage(UserLoginAuthRequestHandler):

    def get(self):
        imageid = int(self.request.get('imageid'))

        item = ImageFile.get_by_id(imageid)

        path = item.path

        gcs_file = gcs.open(path)
        gcs_stat = gcs.stat(path)

        self.response.content_type = gcs_stat.content_type
        self.response.content_type_params = None
        self.response.write(gcs_file.read())
        gcs_file.close()





app = webapp2.WSGIApplication(
                            [
                                webapp2.Route('/user/api/resource/putimage', PutImage,'PutImage'),
                                webapp2.Route('/user/api/resource/getimage', GetImage,'GetImage'),
                            ],
                            debug=Common.isDebug()
)

With this kind of feeling, the registration / editing function of circle items has been added. https://github.com/nagai/freemarket/tree/20140429 Next time, we will create a spot sale venue.

Recommended Posts

Let's make a spot sale service 8 (image uploader edition)
Let's make a spot sale service 2
Let's make a spot sale service 1
Let's make a spot sale service 3
Let's make a spot sale service 4 (in Python mini Hack-a-thon)
Let's make a Discord Bot.
Make Responder a daemon (service)
Let's make a rock-paper-scissors game
Let's make a LINE bot using various services [ngrok edition]
Let's make a remote rumba [Hardware]
Make a Santa classifier from a Santa image
Let's make a remote rumba [Software]
Let's make a GUI with python.
Let's make a breakout with wxPython
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Let's make a shiritori game with Python
Make Jupyter Notebook a service on CentOS
Make Unity Accelerator a service on Linux
Let's make a voice slowly with Python
Let's make a simple language with PLY 1
Let's make a multilingual site using flask-babel
Let's make a web framework with Python! (1)
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a combination calculation in Python
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Let's make a Backend plugin for Errbot
Let's replace UWSC with Python (5) Let's make a Robot
Let's make a module for Python using SWIG