def edit(id, filename): if current_user.id == id: doc = db.File(id, filename) doc.get_human_name() doc.get_description() return render_template('edit.html', doc=doc) abort(404)
def download(id, filename): doc = db.File(id, filename) doc.get_visibility() if doc.visibility: return send_from_directory(f'/home/std/{id}', filename) try: if current_user.id == id: return send_from_directory(f'/home/std/{id}', filename) except AttributeError: abort(404)
def upload_file(user, files, keep_filename): uploaded_size = 0 current_disk_usage = get_current_disk_usage() for f in files: files[f].seek(0, os.SEEK_END) uploaded_size += files[f].tell() files[f].seek(0) if uploaded_size + current_disk_usage >= config.max_usable_disk_space: make_space_on_disk(uploaded_size + current_disk_usage - config.max_usable_disk_space) if user is None: return flask.abort(401) for f in files: file_extension = os.path.splitext(secure_filename( files[f].filename))[1] if not keep_filename: while True: filename = ''.join([ random.choice(string.ascii_lowercase + string.digits) for n in range(13) ]) + file_extension if check_filename_free(filename): break else: filename = files[f].filename if check_filename_free(filename): pass else: filename = handle_filename_collision(filename) files[f].save(os.path.join('uploads/', filename)) filesize = os.path.getsize(os.path.join('uploads/', filename)) db.session.add( db.File(who_uploaded=user.id, filename=filename, uploaded=datetime.utcnow(), filesize=filesize)) db.session.commit() return 'https://{}/'.format( config.files_domain) + urllib.parse.quote(filename)
class DBTaskManager(object): def __init__(self, username, password): self.username = username self.password = password self._last_check_login = 0 self._last_update_task = 0 self._last_update_downloading_task = 0 self._last_get_task_list = 0 #fix for _last_get_task_list self.time = time self._xunlei = LiXianAPI() self.last_task_id = 0 self.islogin = self._xunlei.login(self.username, self.password) self._last_check_login = time() @property def xunlei(self): if self._last_check_login + options.check_interval < time(): if not self._xunlei.check_login(): self._xunlei.logout() self.islogin = self._xunlei.login(self.username, self.password) self._last_check_login = time() return self._xunlei @property def gdriveid(self): return self._xunlei.gdriveid @property def uid(self): return self._xunlei.uid @sqlalchemy_rollback def _update_tasks(self, tasks): session = Session() nm_list = [] bt_list = [] for task in tasks: if task.task_type in ("bt", "magnet"): bt_list.append(task.id) else: nm_list.append(task.id) for res in self.xunlei.get_task_process(nm_list, bt_list): task = self.get_task(res['task_id']) task.status = res['status'] if task.status == "failed": task.invalid = True task.process = res['process'] if res['cid'] and res['lixian_url']: task.cid = res['cid'] task.lixian_url = res['lixian_url'] task = session.merge(task) if task.status in ("downloading", "finished"): if not self._update_file_list(task): task.status = "failed" task.invalid = True session.add(task) session.commit() @sqlalchemy_rollback def _update_task_list(self, limit=10, st=0, ignore=False): session = Session() tasks = self.xunlei.get_task_list(limit, st) for task in tasks[::-1]: if task['status'] == "finished": self.last_task_id = task['task_id'] db_task_status = session.query( db.Task.status).filter(db.Task.id == task['task_id']).first() if db_task_status and db_task_status[0] == "finished": continue db_task = db.Task() db_task.id = task['task_id'] db_task.create_uid = self.uid db_task.cid = task['cid'] db_task.url = task['url'] db_task.lixian_url = task['lixian_url'] db_task.taskname = task['taskname'] db_task.task_type = task['task_type'] db_task.status = task['status'] if db_task.status == "failed": db_task.invalid = True db_task.process = task['process'] db_task.size = task['size'] db_task.format = task['format'] db_task = session.merge(db_task) if not self._update_file_list(db_task): db_task.status = "failed" db_task.invalid = True session.add(db_task) session.commit() @sqlalchemy_rollback def _update_file_list(self, task): session = Session() if task.task_type == "normal": tmp_file = dict(task_id=task.id, cid=task.cid, url=task.url, lixian_url=task.lixian_url, title=task.taskname, status=task.status, dirtitle=task.taskname, process=task.process, size=task.size, format=task.format) files = [ tmp_file, ] elif task.task_type in ("bt", "magnet"): try: files = self.xunlei.get_bt_list(task.id, task.cid) except Exception, e: logging.error(repr(e)) return False for file in files: if file['lixian_url']: self.last_task_id = file['task_id'] db_file = db.File() db_file.id = file['task_id'] db_file.task_id = task.id db_file.cid = file['cid'] db_file.url = file['url'] db_file._lixian_url = fix_lixian_url(file['lixian_url']) db_file.title = file['title'] db_file.dirtitle = file['dirtitle'] db_file.status = file['status'] db_file.process = file['process'] db_file.size = file['size'] db_file.format = file['format'] session.merge(db_file) return True
def edit_post(id, filename): if current_user.id == id: doc = db.File(id, filename) doc.update_description(request.form.get('description')) return redirect(url_for('account', id=id, msg='File changed')) abort(404)
def vis(id, filename): if current_user.id == id: db.File(id, filename).switch(int(request.args.get('val'))) return redirect(url_for('account', id=id, msg='Visibility changed')) abort(404)
def delete(id, filename): if current_user.id == id: f = db.File(id, filename) current_user.delete(f) return redirect(url_for('account', id=id, msg='File deleted')) abort(404)
database.set(root) counters = {'audio': 0} skipped = [] no_mbdata = [] try: for dirpath, dirnames, filenames in os.walk(dir_to_parse, followlinks=True): for filename in filenames: # Determine full path, the base, and the filename # relative to the base; the base concept can be used # to have multiple media file "root"s. fullpath = path.join(dirpath, filename) relpath = path.relpath(fullpath, dir_to_parse) dbfile = db.File(root, relpath) # For now, only process new files print 'Looking at', relpath, if dbfile.key() in database.files: print 'already known' else: print # Open the file try: audio = mutagen.File(fullpath, easy=True) except Exception, e: skipped.append((fullpath, e)) continue
class DBTaskManager(object): def __init__(self, username, password): self.username = username self.password = password self._last_update_all_task = 0 self._last_update_downloading_task = 0 self._last_get_task_list = 0 #fix for _last_get_task_list self.time = time self._last_update_task = 0 self._last_update_task_size = 0 self._last_update_task_lock = Lock() self._xunlei = LiXianAPI() _verifycode = self._xunlei._get_verifycode(self.username) if not _verifycode: with open('verifycode.jpg', 'w') as verifycode_fp: verifycode_fp.write(self._xunlei.verifycode()) _verifycode = raw_input( 'Please open ./verifycode.jpg and enter the verifycode: ') self.islogin = self._xunlei.login(self.username, self.password, _verifycode) self._last_login = time() self._last_check_login = time() @property def xunlei(self): if self._last_login + options.re_login_interval > time() and\ self._last_check_login + options.check_interval > time() and\ self._xunlei.check_login(): self._last_check_login = time() return self._xunlei self.re_login() return self._xunlei def re_login(self): self._xunlei.logout() self.islogin = self._xunlei.login(self.username, self.password) self._last_login = time() self._last_check_login = time() return self.islogin @property def gdriveid(self): return self._xunlei.gdriveid @property def uid(self): return self._xunlei.uid def get_vip(self): return {"uid": self.uid, "gdriveid": self.gdriveid, "tid": 1} @sqlalchemy_rollback def _update_tasks(self, tasks): session = Session() while tasks: nm_list = [] bt_list = [] for task in tasks[:100]: if task.task_type in ("bt", "magnet"): bt_list.append(task.id) else: nm_list.append(task.id) for res in self.xunlei.get_task_process(nm_list, bt_list): task = self.get_task(res['task_id']) if not task: continue task.status = res['status'] task.process = res['process'] if task.status == "failed": task.invalid = True if res['cid'] and res['lixian_url']: task.cid = res['cid'] task.lixian_url = res['lixian_url'] if task.status in ("downloading", "finished"): if not self._update_file_list(task): task.status = "downloading" session.add(task) tasks = tasks[100:] session.commit() session.close() @sqlalchemy_rollback def _update_task_list(self, limit=10, st=0, ignore=False): now = self.time() with self._last_update_task_lock: if now <= self._last_update_task and limit <= self._last_update_task_size: return self._last_update_task = self.time() self._last_update_task_size = limit session = Session() tasks = self.xunlei.get_task_list(limit, st) for task in tasks[::-1]: db_task_status = session.query(db.Task.status).filter( db.Task.id == task['task_id']).first() if db_task_status and db_task_status[0] == "finished": continue db_task = self.get_task(int(task['task_id'])) changed = False if not db_task: changed = True db_task = db.Task() db_task.id = task['task_id'] db_task.create_uid = self.uid db_task.cid = task['cid'] db_task.url = task['url'] db_task.lixian_url = task['lixian_url'] db_task.taskname = unescape(task['taskname']) or "NULL" db_task.task_type = task['task_type'] db_task.status = task['status'] db_task.invalid = True db_task.process = task['process'] db_task.size = task['size'] db_task.format = task['format'] else: db_task.lixian_url = task['lixian_url'] if db_task.status != task['status']: changed = True db_task.status = task['status'] if db_task.status == "failed": db_task.invalid = True if db_task.process != task['process']: changed = True db_task.process = task['process'] session.add(db_task) if changed and not self._update_file_list(db_task, session): db_task.status = "failed" db_task.invalid = True session.add(db_task) session.commit() session.close() @sqlalchemy_rollback def _update_file_list(self, task, session=None): if session is None: session = Session() if task.task_type == "normal": tmp_file = dict(task_id=task.id, cid=task.cid, url=task.url, lixian_url=task.lixian_url, title=task.taskname, status=task.status, dirtitle=task.taskname, process=task.process, size=task.size, format=task.format) files = [ tmp_file, ] elif task.task_type in ("bt", "magnet"): try: files = self.xunlei.get_bt_list(task.id, task.cid) except Exception, e: logging.error(repr(e)) return False for file in files: db_file = session.query(db.File).get(int(file['task_id'])) if not db_file: db_file = db.File() db_file.id = file['task_id'] db_file.task_id = task.id db_file.cid = file['cid'] db_file.url = file['url'] db_file._lixian_url = file[ 'lixian_url'] #fix_lixian_url(file['lixian_url']) db_file.title = unescape(file['title']) db_file.dirtitle = unescape(file['dirtitle']) db_file.status = file['status'] db_file.process = file['process'] db_file.size = file['size'] db_file.format = file['format'] else: db_file._lixian_url = file[ 'lixian_url'] #fix_lixian_url(file['lixian_url']) db_file.status = file['status'] db_file.process = file['process'] session.add(db_file) return True