synology - 通过命令行获取 gofile.me - Synology

标签 synology

我正在用 PHP 编写一个小模块,它将向我的客户发送电子邮件(使用 PHPMailer)。

我不想在这些电子邮件中附加巨大的文件,而是发布一个下载文件的链接(压缩)。

这些文件存储在 DSM6.x 下的 Synology 上。

有没有办法通过 SSH 中的命令行指向所需的文件并使用 Synology 说明生成 gofile.me 链接?

谢谢

最佳答案

这可以通过 Synology REST API 实现.

尽管这可能并不完全是您想要的,但这里有一个用于从命令行创建 gofile.me 链接的 Python 3 脚本。不幸的是,我未能在互联网上找到任何简单的可行解决方案,所以我自己编写了它。希望这会有所帮助(see also my Github repo)。

#=========================================================================
# SynoShare.py                                               (c) Ded, 2020
#=========================================================================
#
# Creates a public http://gofile.me link for path/file located on Synology server.
#
# Usage: SynoShare.py <path / filename_to_share> [--debug]
#
# Copyright (c) 2020. Ded (Ilya Dedinsky, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6cbc7cfcae6c2c3c2959488d4d3" rel="noreferrer noopener nofollow">[email protected]</a>)
#=========================================================================

from __future__ import print_function;
import requests;
import json;
import traceback;
import sys;
import os;

#-------------------------------------------------------------
# These values are imported from OS environment variables to avoid exposing of private data:
#
# SynoShareNAS     - Synology NAS IP or DNS name
# SynoShareAccount - Synology NAS Account used for creating links
# SynoSharePasswd  - Synology NAS Account password
#
# Set these variables BEFORE calling this script. They may be crypted, see below.
#
# NOTE: This script contains Decode() function for decoding login data, which may be crypted.
# Now this function works transparently, simply returning its parameters. You can update this
# function to support your scheme of encrypting login credentials.
#-------------------------------------------------------------

NAS     = str (os.getenv ("SynoShareNAS"));
Account = str (os.getenv ("SynoShareAccount"));
Passwd  = str (os.getenv ("SynoSharePasswd"));

#-------------------------------------------------------------
# Exit codes of the script
#-------------------------------------------------------------

LinkCreated    = 0;
LinkExists     = 1;
LinkNotCreated = 2;
FatalError     = 3;
SyntaxError    = 255;

#-------------------------------------------------------------

Sid = "";

Debug = False;
if (len (sys.argv) >= 3): Debug = (sys.argv[2] == "--debug");

#-------------------------------------------------------------

def main():
    _("", "main()");

    global Debug;
    if (len (sys.argv) >= 3): Debug = (sys.argv[2] == "--debug");

    file = "";
    if (len (sys.argv) >= 2): file = sys.argv[1];

    if (file == ""):
        eprint ("ERROR: Usage: " + sys.argv[0] + " <path/filename to share> [--debug]");
        return SyntaxError;

    if (NAS == "None" or Account == "None" or Passwd == "None"):
        eprint ("ERROR: SynoShareNAS, SynoShareAccount or SynoSharePasswd environment variable(s) NOT found");
        return SyntaxError;

    DoAuth (Account, Passwd);

    list = SharingList();

    links = list["data"]["links"];
    total = list["data"]["total"];

    for (link) in (links):
        if (link["path"] == file):
            url = _(link["url"], "url");

            eprint ("EXISTS:", url);
            print (url);
            return LinkExists;

    url  = "";
    link = _(SharingCreate (file), "link");

    if (link["success"] == True):
        url = _(link["data"]["links"][0]["url"], "url");

        eprint ("CREATED:", url);
        print (url);
        return LinkCreated;

    else:
        eprint ("NOT created:", '"' + file + '"');
        return LinkNotCreated;

#=========================================================================

def GetApiInfo():
    _("\n", "GetApiInfo()");

    return _(Get ("query.cgi?api=SYNO.API.Info&version=1&method=query&query=SYNO.API.Auth,SYNO.FileStation"), "GetApiInfo");

#-------------------------------------------------------------------------

def DoAuth (account, passwd):
    _("\n" + "account = " + account + ", passwd = " + passwd, "DoAuth()");

    try:
        account = Decode (account);
        passwd  = Decode (passwd);

        res = _(Get ("auth.cgi?api=SYNO.API.Auth&version=3&method=login&account=" + account + "&passwd=" + passwd + "&session=FileStation&format=sid"), "DoAuth()");

        global Sid;
        Sid = res["data"]["sid"];

        return _(Sid, "DoAuth(): Sid");

    except Exception as e:
        raise Exception ('DoAuth(): Cannot auth in "' + NAS + '":\n  ' + str (e));

#-------------------------------------------------------------------------

def SharingList():
    _("\n", "SharingList()");

    return _(Get ("entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=list"), "DoSharing");

#-------------------------------------------------------------------------

def SharingCreate (path):
    _("\n" + "path = " + path, "SharingCreate()");

    return _(Get ('entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="' + path + '"'), "DoSharing");

#=========================================================================

def Get (request):
    _("\n" + "request = " + request, "Get()");

    if (Sid != ""): request += "&_sid=" + Sid;

    res = _(requests.get (_("http://" + NAS + "/webapi/" + request, "GET")), "GET");
    if (res.status_code != 200):  raise Exception ("Get (" + request + "): Bad status " + str (res.status_code) + ":\n  " + res.text);

    res = json.loads (res.text);
    if (not res["success"]): raise Exception ("Get (" + request + "):\n" + "  Error: " + StrError (res["error"]["code"]));

    return _(res, "GOT");

#-------------------------------------------------------------------------

def StrError (code):

    errlist = { 400: "No such account or incorrect password",
                401: "Account disabled",
                402: "Permission denied",
                403: "2-step verification code required",
                404: "Failed to authenticate 2-step verification code",
               2000: "Sharing link does not exist",
               2001: "Cannot generate sharing link because too many sharing links exist",
               2002: "Failed to access sharing links" };

    return str (code) + ": " + errlist.get (code, "Unknown error");

#-------------------------------------------------------------------------

def Decode (str):
    _("str = " + str, "Decode()");

#   TODO: add some transformation code here to avoid expose your account name and passwd

    return str;

#=========================================================================

def _ (data = "", name = ""):

    if (not Debug): return data;

    if (str (data) [0:1] == "\n"): data = data[1:]; eprint();

    if (name != ""): eprint (name, ": ", sep = "", end = "");

    if (data != ""): eprint ('"', data, '"', sep = "");
    else: eprint();

    return data;

#-------------------------------------------------------------------------

def eprint (*args, **kwargs):

    print (*args, file = sys.stderr, **kwargs);

#=========================================================================

try:
    sys.exit (main());

except Exception as e:

    eprint ("ERROR: " + str (e));
    if (Debug): eprint ("\n" + traceback.format_exc());
    sys.exit (FatalError);

#=========================================================================

关于synology - 通过命令行获取 gofile.me - Synology,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59680988/

相关文章:

linux - 无法运行脚本

ffmpeg:将传输流(MPEG2 或 h264)重新编码为另一个传输流(h264)

git - jenkins 如何构建/克隆安装在其 docker 容器中的远程 git 存储库?

linux - 如何获取文件仍然为 ftp 打开的信息

mysql - Synology NAS,File Station 应用程序有数据库吗?在哪里可以找到它?

node.js - 如何在 node.js 上安装 fabric.js

mysql - Synology Diskstation 上的 "The service is disabled now."

python - 如何在 Synology 上安装 Pip 3,然后安装 Python Lib MySQLdb

php - MySQL自主重复删除

linux - Shell 比较不适用于 Synology