跳到内容
Syno Package Land
语言: 简体中文

App API 参考

原生 App 有一个 Go 后端;它的 UI 以具名方法调用后端,例如 greeting.get。

ctx.call(method, params) 会带着用户的 DSM 会话,发送同源的 JSON POST。

项目值
请求内容{"method": "app.status", "params": {}}。未知的字段会被拒绝。
成功{"success": true, "data": …}。ctx.call 会以 data 完成。
错误{"success": false, "error": {"code": "…", "message": "…"}}。ctx.call 会以 {code, message} 拒绝。
HTTP 状态一律是 200,DSM 才不会取代响应内容。
来源只接受同源;Content-Type 必须是 application/json。

错误码请见错误码。上传与下载改用查找字符串方法(?method=nas.files.upload、?method=nas.files.download);辅助函数会替你处理。

每个方法有一个等级,设在处理函数之外,也就是 backend/internal/api/permissions.json。

等级谁可以调用
administratorsDSM 管理员群组的成员。这是默认值。
authenticated任何通过 entry 检查的已登录 DSM 用户。
public任何人,不需登录。只有 identity.* 方法使用。

没有列出的方法一律视为 administrators:非管理员会得到 admin_required,而未注册的方法会得到 unknown_method。

{
"schema": 1,
"default": "administrators",
"methods": {
"app.status": "authenticated",
"reports.list": "authenticated"
}
}
规则值
文件结构恰好是 schema、default 与 methods;default 是 administrators。
方法名称小写的 area.action,例如 reports.list。格式 ^[a-z][a-z0-9_]*(\.[a-z][a-z0-9_]*)+$,最多 128 个字符。
大小最多 1024 个方法与 64 KiB;重复的 key 会被拒绝。
已注册的方法列出的每个方法都必须有处理函数。
能力的方法nas.*、bg.*、identity.*、portal.*、executor.*、root.run 与 delegate.* 的权限是固定的。

check 与 build 会验证这个文件;错误使用 STANDALONE_API_PERMISSIONS_INVALID。

dsm-package.yaml 的 entry 字段限制谁可以打开 App;管理员一律通过,public 方法则略过(Manifest 参考)。

entry效果
省略没有额外检查。能打开窗口的用户都可以调用 authenticated 方法。
admin-only只有管理员;其他人会得到 admin_required。
app-privilege只有 DSM 应用程序权限允许的用户;其他人会得到 app_privilege_denied。至少需要一个不是 administrators 的方法;有 files 或 jobs 就足够。

以 synopkgland add <name> 或 init --with <name> 加入能力。

方法权限用途
app.statusadministrators套件名称、版本、调用者(user、admin)、进程、平台与时间。
greeting.getadministrators读取范例问候语。返回 {text}。
greeting.setadministrators写入范例问候语。{text} 必须是 1 到 200 个字符。

这三个是范例,请替换掉。init --with delegate 也会把 app.status 列为 authenticated。

方法权限参数用途
nas.session.statusauthenticated无文件访问打开时,返回 {mode: "filestation-session"}。
nas.files.sharesauthenticatedoffset列出用户看得到的共享文件夹。
nas.files.listauthenticatedpath、offset、limit(最多 100)、sortBy(name、size、type、mtime)、sortDirection(ASC、DESC)列出文件夹内容。
nas.files.infoauthenticatedpath返回单一文件或文件夹。
nas.files.create_folderauthenticatedparent、name创建文件夹。返回 {created: true}。
nas.files.uploadauthenticated查找字符串 parent、name。内容是文件。上传文件;不会覆写,也不会创建上层文件夹。需要 Content-Length,最大 16 GiB。
nas.files.downloadauthenticated查找字符串 path以附件形式下载文件。

调用会以用户自己的 DSM 权限运行(文件)。路径必须是绝对路径,最多 4096 个字符,且不含 \、,、" 或控制字符。

方法权限用途
bg.statusauthenticated拥有者、上限,以及排队中与运行中的工作数量。
bg.browseauthenticated浏览调用者的文件夹,以选择工作输入;不需要 files。
bg.jobs.submitauthenticated将工作排入队列。参数 key、folder、relative。返回 {job, duplicate}。
bg.jobs.listauthenticated列出调用者的工作,最新的在前。
bg.jobs.getauthenticated以 id 获取一个工作。
bg.jobs.cancelauthenticated以 id 取消工作。
bg.jobs.resultauthenticated已完成工作的结果,格式为 JSON;完成前会得到 not_ready。
bg.jobs.purgeadministrators清除某个用户的工作数据(owner: {user, uid})。
bg.schedules.listauthenticated列出调度。
bg.schedules.createauthenticated重复运行工作。参数 key、jobId、intervalSeconds(3600、86400 或 604800)、consent: true。
bg.schedules.disableauthenticated以 id 停止调度。
bg.schedules.deleteauthenticated以 id 删除调度。
bg.schedules.enableadministrators重新启动调度。参数 id、consent: true。

工作的 key 符合 ^[A-Za-z0-9._-]{1,64}$。状态有 queued、running、succeeded、failed、canceled 与 interrupted;失败的工作会有 errorCode(背景工作)。

方法参数用途
identity.status无返回 {enabled: true}。
identity.sessionbootstrap(选用)返回 App 会话的 {user, uid, expiresAt}。使用 bootstrap: true 且没有 App 会话时,会从 DSM 登录状态创建一个。
identity.loginaccount、password、otp(选用)以用户的 DSM 帐号登录,并创建 App 会话。
identity.logout无结束 App 会话。返回 {loggedOut: true}。

所有 identity.* 方法都是 public,且需要 HTTPS 与同源的 JSON POST(登录)。

方法权限用途
portal.statusauthenticated读取已保存的门户网站别名,以及是否已激活。

管理员通过 ctx.portal 设置或清除别名(应用程序门户网站)。

方法权限参数用途
executor.statusauthenticated无已核准辅助程序的状态:digest、版本、ready、disabled,以及 tasks,也就是 executor 对核准任务的读回结果 {state, checked_at}:checking,之后是 ready、stale(超过 15 分钟)或 failed。启动时读取,通过后每 10 分钟、失败后每分钟读取。暂停的 executor 会返回 {ready: false, state: "paused", reason: "executor_not_listening", next}。
executor.controladministratorsaction:start、stop 或 purge启动、停止或清除辅助程序。
root.runadministratorsname以 root 运行 root/<name>.sh,并返回 {output}。不能带参数。

delegate 项目也会有 executor.*。root.run 失败,或 executor.status 除了暂停的 executor 之外失败,UI 都会收到 internal。Executor 的帐号检查以原生查找双向比对名称/UID,并检查到期与目前的管理员群组成员资格;AD 用户需要可连接的域名控制站,未知的 provider 或查找失败一律拒绝(管理员操作)。

默认的 lifetime: until-revoked 使用这些方法。

方法权限参数用途
delegate.statusauthenticated无状态(ready、disabled、needs-admin-approval、blocked、needs-scope-confirmation)、生命周期、目的地与范围。
delegate.destinationauthenticatedkey单一目的地的状态:active、missing、reselection-required、needs-scope-confirmation 或 disabled。
delegate.select_destinationauthenticatedkey、path选择目的地文件夹:先检查写入权限,再保存授权。
delegate.move_from_stagingauthenticateddelegation_id、job_id、staging、name、size、选用 conflict把完成的文件从暂存区移到选定的文件夹。name 可以包含文件夹,例如 Album/CD1/01.flac;conflict 为 fail(默认)、overwrite 或 rename。
delegate.operations.getauthenticateddelegation_id、job_id移动的状态。
delegate.operations.cancelauthenticateddelegation_id、job_id取消移动。
delegate.operations.listauthenticated无(发送 {})列出移动。
delegate.example.exportauthenticatedkey、job_id稍后保存面板使用的范例导出。
delegate.disable、delegate.enableadministrators无关闭或打开这项集成。两者都会结束所有授权。
delegate.auditauthenticated无套件事件;管理员看得到全部,其他人只看得到自己的。最多 256 笔。

没有 lifetime 时,改用 delegate.create(job_id、ops、path、ttl_seconds 为 1 到 3600)、delegate.list、delegate.revoke、delegate.jobs.submit、delegate.jobs.list 与 delegate.jobs.cancel,全部都是 authenticated;它们需要 DSM 会话 cookie 与 token,否则会得到 session_required(Delegate)。

这些操作由 executor 协定 v1 的 schema 产生;? 代表选用参数,需要 DSM 会话的操作会以 AUTH_DENIED 拒绝(Executor 协定错误码)。

操作DSM 会话参数
delegate.audit必要—
delegate.create必要job_id?, ops, scope, path?, ttl_seconds?, destination?, lifetime?
delegate.disable必要—
delegate.enable必要—
delegate.list必要—
delegate.revoke选用delegation_id, job_id?
files.cancelOperation选用delegation_id, job_id
files.checkPermission选用delegation_id, job_id, path
files.copy选用delegation_id, job_id, path, dest_dir, conflict?
files.createFolder选用delegation_id, job_id, path, name, parents?
files.delete选用delegation_id, job_id, path, recursive?, permanent?
files.info选用delegation_id, job_id, path
files.list选用delegation_id, job_id, path
files.move选用delegation_id, job_id, path, dest_dir, conflict?
files.moveFromStaging选用delegation_id, job_id, staging, name, size, conflict?
files.operationStatus选用delegation_id, job_id
files.read选用delegation_id, job_id, path, max_bytes?
files.rename选用delegation_id, job_id, path, name
files.taskCancel选用delegation_id, job_id, task_id
files.taskStatus选用delegation_id, job_id, task_id
files.write选用delegation_id, job_id, staging, dest_dir, name, conflict?
portal.register必要—
portal.unregister必要—
root.jobs.get必要job_id
root.jobs.list必要—
root.jobs.stop必要job_id
root.run必要name, params?
root.start必要name, params?
status选用—
webapi.call选用response_type?, delegation_id, job_id?, api, method, version, params

在 backend/internal/app/routes.go 注册处理函数,然后在 permissions.json 设置它的权限。

// routes.go: add to the list in Routes
api.HandleRequest("reports.list", cfg.listReports),
reports.go
package app
import (
"context"
"encoding/json"
"net/http"
"github.com/synopackageland/syno-pkg-land-sdk/api"
"github.com/synopackageland/syno-pkg-land-sdk/filestation"
)
func (cfg Config) listReports(ctx context.Context, caller api.Caller, r *http.Request, params json.RawMessage) (any, error) {
var in struct {
Folder string `json:"folder"`
}
if err := decodeParams(params, &in); err != nil {
return nil, err
}
if in.Folder == "" {
return nil, api.BadRequest("folder is required")
}
return cfg.SessionFiles.List(ctx, caller, r, in.Folder, filestation.ListOptions{Limit: 50})
}
函数处理函数签名用途
api.Handle(method, h)func(ctx, caller api.Caller, params json.RawMessage) (any, error)大多数 route。
api.HandleRequest(method, h)在 ctx 之后加上 *http.Request。以用户会话调用 File Station 或 executor 的 route。
api.HandleResponse(method, h)加上 http.ResponseWriter 与 *http.Request。返回 error。设置 cookie 或自行写入响应的 route。
api.HandleStream(method, h)func(ctx, caller, w, r) error上传与下载。方法来自查找字符串,内容不是 JSON。

返回值会成为 data;route 的权限一律来自 permissions.json。

项目用途
caller.User()DSM 用户名。
caller.Admin()DSM 管理员为 true。
caller.Authenticated()有用户登录时为 true。
api.RequireAdmin(caller)调用者不是管理员时返回 admin_required;用它保留共用 route 的一部分。
api.BadRequest(msg)返回带有你消息的 bad_request。
&api.Error{Code: "…", Message: "…"}你自己的错误码;UI 看得到。
其他任何 error记录在 NAS 上,并以 internal 返回给 UI。

拥有者请从 caller.User() 获取;不要信任 params 里的用户名。

你需要使用
以用户的权限访问文件cfg.SessionFiles.List、Info、CreateFolder、Upload 与 Download,每个都接受 ctx、caller 与请求 r。需要 files。
实时请求或 delegate 工作共用一个界面filestation.RequestContext{Caller, Request, Service}.Files(),提供 List、Info、CreateFolder、CheckPermission 与 Write。需要 delegate。
delegate 工作的文件操作delegate.JobContext.Files() 另外提供 CreateFolderAll、Rename、Read,以及任务操作 Copy、Move、Delete、TaskStatus 与 CancelTask。每个都需要在 delegate 的 operations 中声明对应操作。
背景工作从 UI 调用 bg.* 方法。worker 固定不变,并会计算 SHA-256。
之后在没有用户会话时保存文件在用户还在时调用 service.Destination(ctx, caller, req, "save"),保留 Destination.ID,之后调用 service.MoveFromStaging(ctx, MoveParams{DelegationID, JobID, Staging, Name, Size})。
root 操作管理员的 UI 以 root/<name>.sh 的脚本名称调用 root.run;产生的 UI 都不会调用它,请自己调用 ctx.call('root.run', {name})。

ctx 是 DSM 窗口的 context。其他辅助函数来自 sdk(@synopackageland/sdk,headless)与 sdk/vue(@synopackageland/sdk/vue;@synopackageland/sdk/vue/style.css 导入一次),版本须为 backend/go.mod 的 SDK 版本,否则 check 会返回 SDK_VERSION_MISMATCH;@synopackageland/ui 只有视觉组件。

辅助函数位置用途
ctx.call(method, params)ctx调用后端方法;以 data 完成,以 {code, message} 拒绝。
ctx.localectxDSM 语言,例如 en 或 zh-TW。
ctx.reopen(fn)ctx每次显示或重新启动时运行 fn;返回移除函数。
ctx.launchParamsctx打开窗口那次启动的字符串值:桌面链接 launchParam 或 SYNO.SDS.AppLaunch 参数。不可信。
ctx.onLaunch(fn)ctx之后每次带值的启动都运行 fn(params);返回移除函数。
ctx.hostctx窗口与文件管理器动作:setTitle、openApp、openURL、openFileManager、revealFile、subscribe。
ctx.userctx{name, isAdmin};用它隐藏控制项,但访问由后端决定。
ctx.filesctx文件传输用的 {endpoint, token};有 files 时才有。
ctx.portalctxstatus()、apply(alias)、disable()、openControlPanel()、mountPanel(el);有 portal 时才有。
ctx.executorctx管理员核准:status()、authorize(password, scope?)、control(action);有 rootOps 或 delegate 时才有。
ctx.t(key, params)ctx来自 i18n/messages 的翻译文字。
followLaunchRoute(ctx, navigate)sdk以这次与之后每次启动的 route 值调用 navigate(route)(原生 App 指南)。
launchRoute(params)、dsmLaunchURL(origin, packageId, params)sdk从启动值读出安全的 App route;创建 DSM 桌面链接。
createNasProvider(ctx)sdk供选择器使用的文件 provider:shares、list、info、createFolder、upload、download。
createSession(ctx)sdk/vue登录会话的状态与调用:login、bootstrap、logout、call。
createBackgroundSaveFacade(ctx)sdk为稍后保存面板包装 delegate.*。
FilesPage、FileBrowser、NasPickersdk/vueVue 文件与文件夹浏览。
LoginPagesdk/vueVue 登录表单;需要 https:。
BackgroundPanelsdk/vue以 bg.browse 为基础的 Vue 工作面板。
ExecutorSettingssdk/vue管理员核准背景访问的 Vue 面板。
ExecutorApprovalsdk/vue在 executor 核准前先保留页面。加上 preload 时,页面会隐藏挂载并同时读取状态;ready 时保留该实例,其他状态则丢弃。
useExecutorPreload()sdk/vue预载期间为 true;它会在页面卸载前变成 false,所以请在 watchPostEffect 中打开 teleport 的对话框或 toast。AuthorizationGate 会自行等待。
PortalSettingssdk/vue门户网站别名的 Vue 面板。
// A Vue component with ctx as a prop
const list = await ctx.call<{ files: { name: string; isdir: boolean }[] }>('reports.list', {
folder: '/volume1/reports',
})
限制值
请求内容1 MiB
单一请求的时间20 秒
App 登录会话最长 15 分钟
登录尝试每个帐号每分钟 5 次,每个地址每分钟 20 次

ctx.batch 与 createClient().batch 会在一次 HTTP 请求中发送 1 到 8 个独立的 JSON 方法。

const results = await ctx.batch!([
{method: 'app.status'},
{method: 'greeting.get'},
])

结果依输入顺序为 {success: true, data} 或 {success: false, error};每个方法仍保留自己的权限检查,单一失败不影响其他结果。传输失败会拒绝整个批量。bridge 方法会共用一个 App 进程。运行顺序未定,串流与 cookie route 会被拒绝,批量也从不重试,因此有依赖性的写入请分开调用;createSession(ctx).batch 处理会话与拒绝的方式与 call 相同。