def info(): shares = [] share_names = oscar.share_names() for share_name in share_names: share = oscar.get_share(share_name) comment = share.comment if share.comment else (u"共有フォルダ" if share.guest_ok else u"アクセス制限された共有フォルダ") if share.guest_ok or (flask.g.username and oscar.check_user_credential(share_name, flask.g.username)): share_info = {"name":share.name,"comment":comment,"guest_ok":share.guest_ok} shares.append(share_info) # 共有フォルダが存在するがいずれもアクセス可能な権限を持たない場合 or # プライベートでないIPアドレスからアクセスされている場合 認証必要 if ((len(share_names) > 0 and len(shares) == 0) or not is_private_network()) and not flask.g.username: raise AuthRequired() st = os.statvfs(samba.get_share_folder_base()) def capacity_string(nbytes): if nbytes >= 1024 * 1024 * 1024 * 1024: return "%.1fTB" % (nbytes / (1024 * 1024 * 1024 * 1024.0)) if nbytes >= 1024 * 1024 * 1024: return "%.1fGB" % (nbytes / (1024 * 1024 * 1024.0)) if nbytes >= 1024 * 1024: return "%.1fMB" % (nbytes / (1024 * 1024.0)) free_bytes = st.f_bavail * st.f_frsize total_bytes = st.f_blocks * st.f_frsize used_bytes = (st.f_blocks - st.f_bfree) * st.f_frsize capacity = { "used" :( capacity_string(used_bytes), used_bytes * 100 / total_bytes ), "total" : ( capacity_string(total_bytes), 100 ) } capacity["free"] = ( capacity_string(free_bytes), 100 - capacity["used"][1] ) return flask.jsonify({"loadavg":os.getloadavg(),"capacity":capacity,"shares":shares,"eden":is_eden(flask.request)})
def exec_search(share_name): share = oscar.get_share(share_name) if share == None: return "Share not found", 404 check_access_credential(share) path = flask.request.args.get("path") or "" q = flask.request.args.get("q") offset = int(flask.request.args.get("offset") or "0") limit = int(flask.request.args.get("limit") or "20") if q == "" or q == None: return flask.jsonify({"count":0, "rows":[]}) start_time = time.clock() with oscar.context(share.real_path("/")) as context: result = search.search(context,path,q, offset, limit) for row in result["rows"]: path = oscar.remove_preceding_slash(row["path"].encode("utf-8")) row["exists"] = os.path.exists(os.path.join(share.path, path,row["name"].encode("utf-8"))) search_time = time.clock() - start_time result["q"] = q result["time"] = search_time #time.sleep(3) return flask.jsonify(result)
def get_file(share_name, path,filename): share = oscar.get_share(share_name) if share == None: return "Share not found", 404 check_access_credential(share) return flask.send_from_directory(share.real_path(path), filename.encode("utf-8"))
def delete_share_folder(share_name): share = oscar.get_share(share_name) if not share: return (False, "SHARENOTEXIST") if not os.path.isdir(share.path): return (False, "DIRNOTEXIST") rst = oscar.remove_share(share_name) shutil.rmtree(share.path, ignore_errors=True) return (rst, None)
def create_share_folder(share_name, comment, options=None, share_dir=None): if oscar.get_share(share_name): return (False, "SHAREALREADYEXISTS") if share_dir: base_dir = os.path.dirname(share_dir) else: base_dir = _share_folder_base share_dir = os.path.join( base_dir, share_name.encode("utf-8") if isinstance(share_name, unicode) else share_name) if os.path.exists(share_dir): return (False, "DIRALREADYEXISTS") if not os.path.isdir(base_dir) and not os.access(base_dir, os.W_OK): return (False, "NOACCESS") os.mkdir(share_dir) init.init(share_dir) if options: config.put_all(share_dir, options) rst = oscar.register_share( oscar.Share(share_name, share_dir, comment=comment, guest_ok=True, writable=True)) return (rst, None)
def share_index(share_name): share = oscar.get_share(share_name) if share == None: return "Share not found", 404 if not os.path.isdir(share.real_path("/")): return "Dir not found", 404 check_access_credential(share) return flask.render_template("share.html",share_id=share.name,license=license.get_license_string())
def share_get(share_name): share = oscar.get_share(share_name) if not share: return "Share not found", 404 return flask.jsonify({ "name": share.name, "comment": share.comment, "options": share.options })
def get_path_map(): global _smb_conf_time _smb_conf_time = os.stat(_smb_conf).st_mtime path_map = {} for share_name in oscar.share_names(): share = oscar.get_share(share_name) path_map[share.path] = share return path_map
def share_log(share_name): share = oscar.get_share(share_name) if not share: return "Share not found", 404 category = flask.request.args.get("category") or None offset = int(flask.request.args.get("offset") or "0") limit = int(flask.request.args.get("limit") or "20") return flask.jsonify(log.get_log(share.path, category, offset, limit))
def update_share_folder(share_name, comment, options): share = oscar.get_share(share_name) if not share: return (False, "SHARENOTEXIST") share_dir = share.real_path("/") if not os.path.isdir(share_dir): return (False, "DIRNOTEXIST") config.put_all(share_dir, options) share.comment = comment rst = oscar.update_share(share) return (rst, None)
def setup_new_scheduler(): sched = apscheduler.schedulers.background.BackgroundScheduler( coalesce=True) for path in map(lambda x: oscar.get_share(x).path, oscar.share_names()): syncday = config.get(path, "syncday") if not syncday or not isinstance(syncday, dict): continue if not any(map(lambda (x, y): y, syncday.items())): continue synctime = config.get(path, "synctime") if not synctime or not re.match(r'^\d\d:\d\d$', synctime): continue dow = ','.join( map(lambda (x, y): x, filter(lambda (x, y): y, syncday.items()))) hour, minute = map(lambda x: int(x), synctime.split(':')) oscar.log.debug(u"path=%s day=%s time=%02d:%02d" % (path.decode("utf-8"), dow, hour, minute)) sched.add_job(sync, apscheduler.triggers.cron.CronTrigger(day_of_week=dow, hour=hour, minute=minute), args=[path]) return sched
def share_dir(share_name): share = oscar.get_share(share_name) if share == None: return "Share not found", 404 path = oscar.remove_preceding_slash(flask.request.args.get("path") or "") if not os.path.isdir(share.real_path(path)): return "Dir not found", 404 check_access_credential(share) offset = int(flask.request.args.get("offset") or "0") limit = int(flask.request.args.get("limit") or "20") root, dirs, files = next(os.walk(share.real_path(path))) dirs = filter(lambda x:not x["name"].startswith('.'), map(lambda x:{"name":x if isinstance(x,unicode) else x.decode("utf-8"), "is_dir":True}, dirs)) files = filter(lambda x:not x["name"].startswith('.'), map(lambda x:{"name":x if isinstance(x,unicode) else x.decode("utf-8"), "is_dir":False}, files)) rst = { "count":len(dirs) + len(files), "limit":limit, "rows":(dirs + files)[offset:offset + limit] } return flask.jsonify(rst)
def share_info(share_name): share = oscar.get_share(share_name) if share == None: return "Share not found", 404 path = oscar.remove_preceding_slash(flask.request.args.get("path") or "") if not os.path.isdir(share.real_path(path)): return "Dir not found", 404 check_access_credential(share) if path != "" and not path.endswith("/"): path = path + "/" with oscar.context(share.real_path("/")) as context: with oscar.command(context, "select") as command: command.add_argument("table", "Files") if path != "": command.add_argument("filter", "path @^ \"%s\"" % oscar.escape_for_groonga(path)) command.add_argument("limit", "0") count = json.loads(command.execute())[0][0][0] with oscar.command(context, "select") as command: command.add_argument("table", "FileQueue") command.add_argument("limit", "0") queued = json.loads(command.execute())[0][0][0] return flask.jsonify({"share_name":share_name,"count":count,"queued":queued,"eden":is_eden(flask.request)})
def truncate_share_folder_index(share_name): share = oscar.get_share(share_name) if not share: return (False, "SHARENOTEXIST") if not os.path.isdir(share.path): return (False, "DIRNOTEXIST") rst = truncate.truncate(share.path) return (rst, None)