在使用者離線時代為工作
狀態:delegate 已實作;實機上的裝置生命週期驗收仍待完成。預設授權允許一項操作:files.moveFromStaging。
delegate 讓你的套件在使用者關閉瀏覽器後,仍能替該 DSM 使用者儲存檔案。管理員核准一次儲存範圍,每位使用者在其內選擇目的資料夾,之後你的背景工作會以該使用者本身的權限,把完成的檔案移到該處,直到使用者或管理員撤銷存取。套件絕不以 root 寫入,也絕不替換檔案,除非你要求。
建立含此能力的專案,或把它加入 native-go 專案:
synopkgland init -t native-go --with delegate my-appsynopkgland add delegate兩者都會把這個區塊寫入 dsm-package.yaml;delegate 也會開啟背景工作。
capabilities: delegate: scope: install-selected operations: [files.moveFromStaging] destinations: [save] lifetime: until-revoked| 欄位 | 允許值 |
|---|---|
scope | install-selected:管理員在安裝後選擇 home 或一個共用資料夾。home:每位使用者的家目錄。shared:<name>:一個共用資料夾(不可含 /、\、: 或控制字元)。 |
lifetime | until-revoked |
operations | [files.moveFromStaging] |
destinations | 1 到 64 個唯一鍵,符合 ^[a-z][a-z0-9_-]{0,63}$,不是路徑。 |
install-selected 需要 lifetime: until-revoked;只設定 destinations、lifetime 或 operations 其中一部分時,仍必須把四個值都寫齊。
init --with delegate 會設定 backend/internal/api/permissions.json,add delegate 不會。若要讓一般使用者使用,請在該檔設定 "app.status": "authenticated";方法預設為 administrators。
install-selected 專案第一次 synopkgland deploy 必須帶 --param delegate_share=<Shared Folder> 或 --param delegate_home=true,否則會在建置前以 DEPLOY_PARAM_REQUIRED 失敗。升級會保留已確認的範圍。synopkgland status 會把核准回報為 ready、needs-admin-approval 或 blocked;使用 identity: standalone 時,請用 https:// 主機,因為 status 會透過需要 HTTPS 的 App 工作階段讀取 executor。
每個人看到什麼
Section titled “每個人看到什麼”設定頁使用 BackgroundSaveSettings:每個目的鍵各有一列儲存資料夾,並有選擇資料夾與撤銷存取。
| 誰 | 會發生什麼 |
|---|---|
管理員,install-selected | 選擇 home 範圍或共用資料夾根目錄並輸入使用者密碼;該次提交會透過 executor root task 核准範圍、讀回範圍,並把該根目錄設為自己的目的。 |
| 一般使用者 | 透過受範圍限制的 NAS picker,在已核准範圍內選擇資料夾;系統會先檢查即時 DSM 工作階段與寫入權限,再建立其授權;不需要密碼或第二次同意。 |
使用者,固定 home 或 shared:<name> | 在 manifest 的範圍內選擇目的。 |
| 管理員,儲存已透過 API 停用 | 勾選啟用背景儲存;使用者需重新選擇。 |
開啟設定會保留既有的目的;新的已確認範圍會撤銷所有授權並取消待處理的儲存,但已儲存的檔案會保留。撤銷存取只影響目前登入的使用者。
plain UI 會在 DSM 視窗自己的面板中核准,該面板不會送出範圍,因此只適用於 home 或 shared:<name>;install-selected 需要 Vue UI。init --ui plain --with delegate 會以 CAPABILITY_COMBINATION_INVALID 失敗,check 也會在 plain UI 下拒絕該範圍。
backend/internal/app/delegate.go 會從 github.com/synopackageland/syno-pkg-land-sdk/delegate 建立 delegate.Service 並註冊其路由。它唯一可用的 producer,delegate/example.go,方法 delegate.example.export,會由 BackgroundSavePanel 透過需自行啟用的 sampleSave 旗標提供。收到確認不代表檔案已儲存:
- 在使用者工作階段仍有效時,用
svc.Destination(ctx, caller, request, "save")查出目的,並把Destination.ID留在背景工作記錄中。 - 以模式
0600在svc.StagingDir產生檔案。 - 之後在沒有工作階段的情況下,用
svc.MoveFromStaging移動它;該方法拒絕以 root 執行。
selected, err := svc.Destination(ctx, caller, request, "save")// ... write the finished file into svc.StagingDir as <name> ..._, err = svc.MoveFromStaging(ctx, delegate.MoveParams{ DelegationID: selected.Destination.ID, JobID: jobID, Staging: name, Name: name, Size: size,})重複的 JobID 會回傳原始操作;每個檔案請用新的。
你的 UI 會用 ctx.call 呼叫這些方法;每個都需要有效的 DSM 工作階段,否則會得到 session_required。參數與權限見 API 參考;delegate.disable 與 delegate.enable 是會終止所有授權的管理員方法。
儲存會以 committed 結束並附上 receipt({path, size}),或結束為 canceled、interrupted、uncertain。不要自動重試最後兩者:請先檢查目的。使用者取消會回報 operation-canceled-by-user、grant-revoked、scope-changed、integration-disabled 或 destination-replaced;executor 停止會回報 grant-revoked、validation-denied、integration-disabled 或 storage-error,並移除部分檔案。
子資料夾與名稱衝突
Section titled “子資料夾與名稱衝突”Name 可以指定目的下的資料夾,例如 Album/CD1/01.flac。缺少的資料夾會以使用者身分、依其 DSM 權限建立,因此擁有者與繼承的 ACL 都與 File Station 資料夾一致;既有資料夾會重複使用,取消的移動不會留下任何資料夾。
| 規則 | 值 |
|---|---|
| 分隔符號 | /;開頭或結尾不可有 /,不可有空、. 或 .. 片段。 |
| 資料夾名稱 | 1 到 255 位元組,不可含控制字元,也不可有 \ : * ? " < > |;不可為 #recycle、#snapshot 或 @eaDir(不分大小寫)。 |
| 檔名 | 與不含資料夾的名稱規則相同。 |
| 資料夾,整個名稱 | 最多 16 層、1024 位元組。 |
| Unicode | 依原樣儲存,不正規化;若要對應 File Station 中由 Windows 輸入的名稱,請送 NFC。 |
| 符號連結與掛載 | 名稱中的符號連結、檔案或其他儲存空間會以 destination-not-directory 拒絕。 |
Conflict 決定檔名已被占用時的行為:
Conflict | 效果 |
|---|---|
fail 或空值 | 預設;以 destination-exists 失敗。 |
overwrite | 先以暫時名稱寫入,再重新命名覆蓋舊檔,因此失敗的移動會保留完整舊檔;只會替換可寫的一般檔案。 |
rename | 發布 name (1).ext 到 name (99).ext;receipt.path 會回報使用的名稱。 |
搭配 daemon
Section titled “搭配 daemon”只有在 delegate 使用 lifetime: until-revoked 時,daemon 才能與 delegate 的背景工作並行。daemon 欄位見 manifest 參考。
若未設定 destinations、lifetime 與 files.moveFromStaging,delegate 即為限時:operations 可為下列檔案操作的任何唯一子集,scope 為 home 或 shared:<name>。delegate.create 接受 1 到 3600 的 ttl_seconds 與該授權的操作。產生的 worker 只執行下載與 SHA-256 工作,上限 64 MiB 與 60 秒;其他一律回傳 POLICY_DENIED。它的網址下載只會連到公開網路:loopback、私人、link-local 與 metadata 位址都會被拒絕,DNS 回覆同時混有公開與私人位址也會被拒絕,並且不跟隨轉址。
背景工作透過 job.Files() 呼叫這些操作,所有路徑都必須在授權資料夾內;executor 會在呼叫前檢查每個路徑,並重新檢查 File Station 回報的結果。除了 files.write 與 files.read,它會以使用者身分呼叫 synowebapi,因此 DSM 會套用該使用者的權限,且只有允許清單中的方法能這樣運作。請見讀寫使用者檔案。
| 操作(Go 方法) | 需要 |
|---|---|
files.list, files.info (List, Info) | 讀取 |
files.createFolder (CreateFolder, CreateFolderAll) | 父資料夾的寫入權限;CreateFolderAll 會建立缺少的父資料夾 |
files.checkPermission (CheckPermission) | 無;會寫入並移除一個探測檔案 |
files.write (Write) | 寫入;從 staging 最多 64 MiB |
files.read (Read) | 讀取;最多 64 MiB 的一般檔案 |
files.rename (Rename) | 修改 |
files.copy (Copy) | 來源讀取、目的寫入 |
files.move (Move) | 來源刪除、目的寫入 |
files.delete (Delete) | 刪除 |
規則:
- 宣告你使用的每個操作;寫入絕不會隱含
files.delete。政策外的操作會回傳POLICY_DENIED,授權外的操作則回傳DELEGATION_DENIED。 - 授權資料夾本身不可重新命名、複製、移動或刪除(
SCOPE_DENIED),其外的路徑或經由符號連結的路徑也不行;其內不存在的路徑為not-found。 Copy與Move會拒絕已被占用的名稱,除非你傳入delegate.ConflictOverwrite且沒有rename政策。Delete刪除資料夾需要Recursive: true(recursive-required);沒有資源回收筒時會回傳recycle-bin-disabled,除非設定Permanent: true。
長時間檔案操作
Section titled “長時間檔案操作”Copy、Move 與 Delete 會回傳帶有 TaskID 的 TaskState;請輪詢 TaskStatus,直到 State 為 finished、failed(附 Error)或 canceled。Progress 從 0 到 1,CancelTask 可停止它。只有啟動該工作的背景工作能追蹤它;撤銷授權會停止其工作。
files := job.Files()task, err := files.Move(ctx, "/home/Downloads/album", "/home/Music", "")for err == nil && task.State == "running" { time.Sleep(time.Second) task, err = files.TaskStatus(ctx, task.TaskID)}File Station 會逐項回報拒絕;executor 會回傳該項目的錯誤碼:PERMISSION_DENIED、destination-exists、not-found、name-too-long、NO_SPACE,或在檔案下 CreateFolder 時的 destination-not-directory。這些都無法重試;刪除後仍留下項目會以 PERMISSION_DENIED 或 DSM_FAILED 失敗。
executor 最多追蹤 64 個工作,重新啟動後會回傳 task-not-found;DSM 丟棄工作時 State 為 unknown,請用 Info 確認。把工作回報給後續的 synowebapi 程序,尚未在 NAS 上驗證。
| 限制 | 值 |
|---|---|
| 後端、架構 | 僅 native-go;x86_64、aarch64 與 armv7(armv7 未在 NAS 上驗證) |
| 移動的檔案大小 | 16 GiB |
| 排隊的儲存、並行複製 | 16;1 |
| 單次複製嘗試、追蹤的工作 | 24 小時;64 |
目的鍵、name 中的資料夾 | 1 到 64;16 層與 1024 位元組 |
files.read、conflict: rename | 64 MiB;name (1) 到 name (99) |
| DSM 7.1 上的核准 | 使用 7.1 的 scheduler API;見 Root 操作 |
只有在已核准政策仍相符時,升級才會恢復儲存;否則 app 會停在 needs-admin-approval。變更 scope、destinations、operations、lifetime 或 entry 需要新的管理員核准。解除安裝會移除核准與授權;請見資料與移除。
| 症狀或錯誤碼 | 修正 |
|---|---|
needs-admin-approval | 由管理員在設定中重新核准。 |
needs-scope-confirmation | 由管理員以使用者密碼確認範圍。 |
disabled, revoked, reselection-required | 由管理員啟用儲存,或由使用者重新選擇目的。 |
destination-exists, invalid-destination | 用新的 JobID 搭配新名稱、設定 conflict,或修正鍵值。 |
invalid-path, path-too-deep, destination-not-directory | 修正資料夾名稱;使用新的 JobID。 |
PERMISSION_DENIED, NO_SPACE | 修正權限或釋放空間。 |
queue-full, EXECUTOR_PAUSED | 稍候、啟動套件,或核准。 |
session_required, admin_required | 重新登入,或以管理員身分呼叫。 |
其他錯誤碼見錯誤。