def execute(self, sql: str, param: tuple) -> dict: try: db = self._db_pool.getconn() except Exception as e: print("SQL_Pool Error: CONN [%s] %r" % (sql, param)) raise RError(1) try: cursor = db.cursor() except Exception as e: print("SQL_POOL Error: CURS [%s] %r" % (sql, param)) self._db_pool.putconn(db) raise RError(1) try: cursor.execute(sql, param) except psycopg2.Error as e: cursor.close() self._db_pool.putconn(db) print("SQL_POOL Error: Execute [%s] %r" % (sql, param)) print(e.pgerror) raise RError(1) try: result = cursor.fetchall() except psycopg2.ProgrammingError as e: result = [] db.commit() cursor.close() self._db_pool.putconn(db) if not result: result = [] return result
def read(self, size=-1): if self.length == 0: return None split = self.find_split() if not split: return None times = 0 if self.split_id != split['split']: while times < self.config.retry_times: try: self.load_split(split) except DownloadError: print("Download Error at split %s" % self.split_id) times += 1 time.sleep(1) continue break if times == self.config.retry_times: raise RError(2) if self.length < size: size = self.length self.split_data.seek(self.start - self.split.offset) data = self.split_data.read(size) self.start += len(data) self.length -= len(data) return data
def begin(self): try: db = self._db_pool.getconn() except Exception as e: print("SQL_POOL Error: GET CONNECTION") raise RError(1) return RDataBaseConnection(db, self)
def on_post(self, req, resp, path_id=None): db = req.context['sql'] if req.get_param('admin') != RConfig().admin_password: raise RError(403) if not path_id: path_id = db.execute("SELECT * FROM path WHERE parent_id IS NULL", ())[0]['id'] path = db.execute("SELECT * FROM path WHERE id=%s;", (path_id, ))[0] files = set() for k, v in req.params.items(): # print(k, v) reg = re.match("^file\[(\d*)\].*$", k) if reg: files.add(reg.groups(0)) # print(files) for f in files: file_name = req.get_param("file[%s].name" % f) file_path = req.get_param("file[%s].path" % f) file_size = req.get_param("file[%s].size" % f) file_md5 = req.get_param("file[%s].md5" % f) file_type = req.get_param("file[%s].content_type" % f) file_id = str(uuid.uuid4()) new_file_path = RConfig().work_dir + RConfig( ).upload_path + file_id os.rename(file_path, new_file_path) db.execute( "INSERT INTO path(id, parent_id, type, name, create_at, path, status, size, mime, md5) VALUES " "(%s, %s, 1, %s, now(), %s, 1, %s, %s, %s)", (file_id, path_id, file_name, path['path'] + "/" + file_name, file_size, file_type, file_md5))
def load_split(self, split): self.split_id = split['split'] self.split = DSplit() self.split.offset = split['split_offset'] self.split.length = split['split_length'] self.split.crc = split['split_crc'] self.split.block_id = split['block_id'] self.split.block_offset = split['block_offset'] result = self.db.execute("SELECT * FROM cache WHERE block_id=%s AND upload>-1;", (self.split.block_id,)) if result: self.split_data = io.BytesIO() path = self.config.work_dir + self.config.block_path + result[0]['block_id'] with open(path, "rb") as block_data: block_data.seek(self.split.block_offset) tmp = block_data.read(self.split.length) if zlib.crc32(tmp) != self.split.crc: raise RError(2) if not tmp: return self.split_data.write(tmp) self.split_data.seek(0) else: result = self.db.execute("SELECT * FROM block WHERE id =%s AND status>0; ", (self.split.block_id,)) self.split_data = io.BytesIO() if not result: return result = result[0] file_id = result['file_id'] service = RAuth().get_credential(result['auth_id']) request = service.files().get_media(fileId=file_id) request.headers['Range'] = "bytes=%s-%s" % \ (self.split.block_offset, self.split.block_offset + self.split.length - 1) try: print("Get Split %s at Block %s in %s." % (self.split_id, self.split.block_id, str(request.headers['Range']))) tmp = request.execute() if zlib.crc32(tmp) != self.split.crc: raise RError(2) self.split_data.write(tmp) self.split_data.seek(0) self.db.execute("UPDATE block SET status='%s' WHERE file_id = %s", (self.config.re_upload_limit, file_id)) except Exception: RDateBasePool().execute("UPDATE block SET status=status-1 WHERE file_id = %s", (file_id,)) raise DownloadError(self.split.block_id)
def execute(self, sql: str, param: tuple) -> dict: try: self.cursor.execute(sql, param) except psycopg2.Error as e: self.cursor.close() self.pool.end(self.db) if e.pgcode == '22P02': raise RError(1) print("SQL ERROR: Execute Error Execute [%s] %r" % (sql, param)) print(e.pgerror) raise RError(1) try: result = self.cursor.fetchall() except psycopg2.ProgrammingError as e: result = [] if not result: result = [] return result
def __init__(self, db, pool: RDateBasePool): self.db = db self.pool = pool try: self.cursor = self.db.cursor() except Exception as e: print("SQL ERROR: GET CURSOR ERROR.") self.pool.end(self.db) raise RError(1)
def executemany(self, sql: str, param: tuple): try: self.cursor.executemany(sql, param) except psycopg2.Error as e: self.cursor.close() self.pool.end(self.db) print("SQL ERROR: Execute Error Execute [%s] %r" % (sql, param)) print(e.pgerror) raise RError(1) return True
def on_get(self, req, resp, file_id): db = req.context['sql'] result = db.execute("SELECT * FROM path WHERE id=%s", (file_id, )) if not result: raise RError(404) result = result[0] resp.set_header( "content-disposition", "inline; filename*=UTF-8\'\'%s" % quote(result['name'], encoding='utf-8')) resp.set_header("content-type", result['mime']) resp.set_header("Cache-Control", "max-age=864000") resp.set_header("file_md5", result['md5']) if req.range: start = req.range[0] if req.range[1] >= start: length = req.range[1] - start + 1 elif req.range[1] < 0: length = result['size'] - req.range[0] + req.range[1] + 1 else: raise RError(400) resp.status = falcon.HTTP_206 resp.set_header("Cache-Control", "") resp.set_header( "Content-Range", "bytes %s-%s/%s" % (start, start + length - 1, result['size'])) else: start = 0 length = result['size'] if result['status'] == 1: path = RConfig().work_dir + RConfig().upload_path + result['id'] stream = open(path, "rb") stream.seek(start) else: stream = DFile(file_id, db, start, length) resp.set_header("content-length", length) resp.set_stream(stream, length)
def __init__(self, file_id: str, db: RDataBaseConnection, start: int = 0, length: int = -1): io.RawIOBase.__init__(self) self.config = RConfig() self.db = db self.start = start self.length = length self.splits = list(db.execute("SELECT * FROM file WHERE id =%s ORDER BY split DESC", (file_id,))) if not self.splits: raise RError(404) RDateBasePool().execute("UPDATE cache SET priority=priority+%s " "WHERE upload>-1 AND block_id IN (SELECT block_id FROM file WHERE id = %s)", (self.config.add_priority, file_id)) RDateBasePool().execute("INSERT INTO cache(block_id, priority, upload) " "SELECT block_id, '%s',-1 FROM file WHERE id = %s " "AND block_id NOT IN(SELECT block_id FROM cache) GROUP BY block_id;", (self.config.default_priority, file_id)) self.size = self.splits[0]['size'] self.split_id = -1 self.split_data = io.BytesIO() self.split = None
def on_get(self, req, resp, path_id=None): admin = False if "admin" in req.params and req.params['admin'] == RConfig( ).admin_password: admin = True self._generate_head() db = req.context['sql'] if not path_id: path_id = db.execute("SELECT * FROM path WHERE parent_id IS NULL", ())[0]['id'] path = db.execute("SELECT * FROM path WHERE id=%s;", (path_id, )) if not path: raise RError(404) path = path[0] children = db.execute("SELECT * FROM path WHERE parent_id=%s", (path_id, )) self.text += """<h2 id="pathName">%s</h2>""" % path['name'] self.text += """<h4 id="absolutePath">%s/</h4>""" % path['path'] self.text += """ <table class="table"> <tr> <td>Name</td> <td>Create Time</td> <td>Size</td> </tr> """ if path['parent_id']: if admin: self.text += """ <tr> <td><a href="/path/%s?admin=%s">%s/</a></td> <td>%s</td> <td>%s</td> </tr> """ % (path['parent_id'], RConfig().admin_password, "..", path['create_at'], 0) else: self.text += """ <tr> <td><a href="/path/%s">%s/</a></td> <td>%s</td> <td>%s</td> </tr> """ % (path['parent_id'], "..", path['create_at'], 0) files = [] folders = [] for child in children: if child['type'] == 1: files.append(child) if child['type'] == 0: folders.append(child) sorted(files, key=lambda x: x['name']) sorted(folders, key=lambda x: x['name']) for f in folders: if admin: self.text += """ <tr> <td><a href="/path/%s?admin=%s">%s/</a></td> <td>%s</td> <td>%s</td> </tr> """ % (f["id"], RConfig().admin_password, f["name"], f["create_at"], 0) else: self.text += """ <tr> <td><a href="/path/%s">%s/</a></td> <td>%s</td> <td>%s</td> </tr> """ % (f["id"], f["name"], f["create_at"], 0) for f in files: self.text += """ <tr> <td><a href="/file/%s">%s</a></td> <td>%s</td> <td>%s</td> </tr> """ % (f["id"], f["name"], f["create_at"], self._get_size(f["size"])) self.text += "</table>" if admin: self.text += """ <div class="form-group"> <label for="CreateFolder">Create Folder</label> <input type="text" class="form-control" id="createFolder" placeholder="Folder Name"> </div> <button onclick="s('create','%s','%s')" class="btn btn-default">Submit</button> """ % (path_id, req.params['admin']) self.text += """ <div class="form-group"> <label for="CreateFolder">Delete Item</label> <input type="text" class="form-control" id="deleteItem" placeholder="Item Id"> </div> <button onclick="s('delete','%s','%s')" class="btn btn-default">Submit</button> """ % (path_id, req.params['admin']) self.text += """ <form class="dropzone" id="uploadFile"></form> """ self._generate_end(admin) resp.body = self.text resp.set_header("content-type", "text/html; charset=utf-8")
def error_handle(ex, req, resp, params): if isinstance(ex, falcon.HTTPError): raise ex else: traceback.print_exc() raise RError(0)