Пример #1
0
    def get(self):
        try:
            def task(thread_state):
                print thread_state
                time.sleep(5)

            yield adisp.async(g._thread_pool.add_task)(task)

            reply = u.new_reply()
            u.send_reply(self, reply)
        except:
            u.send_internal_error(self)
        finally:
            self.finish()
Пример #2
0
    def get(self):
        try:
            cols = ["id", "name", "description", "author", "cover"]
            rows = yield u.slide_db.runQuery(
                """SELECT {} FROM packs limit 10""".format(",".join(cols)),
                None
            )
            packs = [dict(zip(cols, row)) for row in rows]

            reply = u.new_reply()
            reply["packs"] = packs
            u.send_reply(self, reply)
        except:
            u.send_internal_error(self)
        finally:
            self.finish()
Пример #3
0
    def get(self):
        try:
            pack_id = self.get_argument("id")

            yield u.slide_db.runOperation(
                """DELETE FROM packs 
                        WHERE id=%s""",
                (pack_id,)
            )

            reply = u.new_reply()
            u.send_reply(self, reply)
        except:
            u.send_internal_error(self)
        finally:
            self.finish()
Пример #4
0
 def post(self):
     try:
         files = self.request.files['files']
         reply = u.new_reply()
         reply["files"] = []
         for f in files:
             result = {}
             result["name"] = f["filename"]
             result["type"] = f["content_type"]
             result["url"] = "aaa/bbb"
             result["size"] = len(f["body"])
             reply["files"].append(result)
         u.send_reply(self, reply)
     except:
         u.send_internal_error(self)
     finally:
         self.finish()
Пример #5
0
    def get(self):
        try:
            pack_id = self.get_argument("id")
            name = self.get_argument("name")
            desc = self.get_argument("desc", "")
            author = self.get_argument("author", "")

            yield u.slide_db.runOperation(
                """UPDATE packs SET name=%s, description=%s, author=%s
                        WHERE id=%s""",
                (name, desc, author, pack_id)
            )

            reply = u.new_reply()
            u.send_reply(self, reply)
        except:
            u.send_internal_error(self)
        finally:
            self.finish()
Пример #6
0
    def get(self):
        try:
            filename = "aa"
            zip_sha1 = "aabbbbcccccddddddd"
            script = """
                local pack_id = redis.call("incr", "pack_id")
                redis.call("hmset", "pack_hash:"..pack_id, "name", "{}", "sha", "{}")
                redis.call("lpush", "pack_list", pack_id)
                return pack_id
                """.format(filename, zip_sha1)

            a = u.redis_script(script)
            print a

            reply = u.new_reply()
            reply["time"] = time.time()
            u.send_reply(self, reply)
        except:
            u.send_internal_error(self)
        finally:
            self.finish()
Пример #7
0
    def post(self):
        try:
            files = self.request.files['files']
            f = files[0]
            filename = f["filename"]

            # make sure tmp dir exists
            if not os.path.exists("tmp"):
                os.makedirs("tmp")

            # save to zip file
            path = "tmp/" + filename
            path = path.encode("utf-8")
            with open(path, 'w') as pf:
                pf.write(f["body"])

            # extract from zip file
            with zipfile.ZipFile(path, 'r') as zf:
                ext = filename.split(".")[-1]
                fn = filename[:-(len(ext)+1)]
                zf.extractall("tmp/")

            # calc sha1
            def sha1file(filepath):
                sha1 = hashlib.sha1()
                with open(filepath, 'rb') as f:
                    sha1.update(f.read())
                return sha1.hexdigest()

            zip_sha1 = sha1file(path)
            to_dir = zip_sha1 + "/"
            from_dir = "tmp/" + filename[:filename.rindex(".")] + "/"

            # upload to oss
            def upload_sample(thread_state):
                print "begin upload sample"
                sample_from = from_dir + "sample.zip"
                sample_to = to_dir + "sample.zip"
                return oss.multi_upload_file(OSS_BUCKET, sample_to, sample_from)
                
            def upload_full(thread_state):
                print "begin upload full"
                full_from = from_dir + "full.zip"
                full_to = to_dir + "full.zip"
                return oss.multi_upload_file(OSS_BUCKET, full_to, full_from)

            def upload_cover(thread_state):
                print "begin upload cover"
                cover_from = from_dir + "cover.jpg"
                cover_to = to_dir + "cover.jpg"
                return oss.multi_upload_file(OSS_BUCKET, cover_to, cover_from)
                
            r1, r2, r3 = yield [adisp.async(g._thread_pool.add_task)(upload_sample), 
                adisp.async(g._thread_pool.add_task)(upload_full),
                adisp.async(g._thread_pool.add_task)(upload_cover)]
            if ((r1.status / 100) != 2) or ((r2.status / 100) != 2) or ((r3.status / 100) != 2):
                raise Exception("upload faild")
            else:
                print "upload ok"

            # parse info
            with open(from_dir + "info.json") as f:
                info = json.load(f)

            # redis
            script = """
                local pack_id = redis.call("incr", "pack_id")
                redis.call("hmset", "pack_hash:"..pack_id, "name", {}, "sha", {})
                redis.call("lpush", "pack_list", pack_id)
                return redis.call("get", "b")
            """.format(filename, zip_sha1)
            a = u.redis_script(script)
            print a

            # 
            conn = yield u.slide_db.beginTransaction()
            print "bbbbbb"
            try:
                row_nums = yield u.slide_db.runOperation(
                    """ INSERT INTO packs
                        (name, author, sha1) VALUES(%s, %s, %s)"""
                    , filename, info["author"], zip_sha1 
                    , conn
                )
                print "ccccc"
                rows = yield u.slide_db.runQuery("SELECT LAST_INSERT_ID()", None, conn)
                print "dddddd"
                insert_id = rows[0][0]
            except Exception as e:
                print e, 'xxxxxxxxxxx'
            finally:
                print "eeeeeeee"
                yield u.slide_db.commitTransaction(conn)

            print "ffffffff"


            reply = u.new_reply()
            reply["name"] = f["filename"]
            reply["size"] = len(f["body"])
            u.send_reply(self, reply)
        except:
            u.send_internal_error(self)
        finally:
            self.finish()