工作階段檔案
files 能力讓你的 App 以已登入使用者的身分使用 File Station:列出、建立、上傳、下載與選取檔案。DSM 會套用該使用者的權限,套件帳號的權限絕不會取代呼叫者的權限。不需要管理員設定。瀏覽器關閉後存取即結束;若需離線作業,請使用背景工作或 Delegate。
synopkgland init -t native-go --with files my-files既有 Go 專案:synopkgland add files。兩者都會寫入:
capabilities: files: sessionsession 是唯一的值。files 需要執行於 x86_64、aarch64 或 armv7 的 Go 後端;binary 後端無法使用。Vue UI 有「檔案」頁面;純 UI 只有路由。
在 UI 中瀏覽與選取
Section titled “在 UI 中瀏覽與選取”Vue UI 提供 FileBrowser 來瀏覽共用資料夾、資料夾與檔案,以及會發出 selected 的 NasPicker 對話框;來自 @synopackageland/sdk 的 createNasProvider(ctx) 以底下的呼叫同時提供兩者。選取路徑不會授予任何權限:後端會在使用時重新檢查工作階段與權限。路徑與 props 請見 UI 元件。
呼叫 ctx.call(method, params);每次呼叫都會檢查 DSM 工作階段屬於已登入的使用者。
| 方法 | 參數 | 結果 |
|---|---|---|
nas.session.status | {} | { mode: "filestation-session" } |
nas.files.shares | { offset } | 一頁共用資料夾 |
nas.files.list | { path, offset, limit, sortBy, sortDirection } | 一頁項目 |
nas.files.info | { path } | 單一項目 |
nas.files.create_folder | { parent, name } | { created: true } |
limit為 1–100(預設 100);sortBy為name、size、type或mtime;sortDirection為ASC或DESC。- 一頁為
{ files, total, offset };共用資料夾列於shares。項目為{ path, name, isdir, additional: { size } }。 - 路徑為絕對且正規化(不含
.或..),且不含反斜線、逗號、雙引號或控制字元。
檔案以串流傳輸,而非 JSON;createNasProvider 與 FileBrowser 會處理。否則請使用 ctx.files.endpoint 與 ctx.files.token():
上傳 nas.files.upload | 下載 nas.files.download | |
|---|---|---|
| 請求 | POST ?method=nas.files.upload&parent=<folder>&name=<file> | GET 或 POST ?method=nas.files.download&path=<file> |
| 內容 | 位元組,application/octet-stream,附 Content-Length | 無;POST 會以表單欄位傳送 SynoToken |
| 驗證 | X-SYNO-TOKEN 標頭與 DSM id cookie | 同一個工作階段 cookie |
| 成功 | {"success":true,"data":{"uploaded":true}} | 以附件形式傳回的檔案 |
僅限同源。下載失敗會留下截斷的檔案,且沒有 JSON 錯誤;上傳回應遺失時為 commit-uncertain。
在處理常式中讀取檔案
Section titled “在處理常式中讀取檔案”以 api.HandleRequest 註冊 Go 處理常式,在 permissions.json 中列為 authenticated,並把 net/http 加入 routes.go 的 imports。
func (cfg Config) readNote(ctx context.Context, caller api.Caller, r *http.Request, params json.RawMessage) (any, error) { var in struct { Folder string `json:"folder"` Relative string `json:"relative"` } if err := decodeParams(params, &in); err != nil { return nil, err } data, err := cfg.SessionFiles.ReadFile(ctx, caller, r, in.Folder, in.Relative) if err != nil { return nil, err } return map[string]any{"bytes": len(data)}, nil}ReadFile 讀取 Folder 底下的一個一般檔案,上限 512 KiB(超過時為 limit-exceeded);cfg.SessionFiles.Upload 與 Download 對應上述串流呼叫。
| 項目 | 限制 |
|---|---|
| 上傳大小 | 16 GiB,需附 Content-Length(upload_size_required) |
| 單一請求(含傳輸) | 產生的後端為 20 秒;UI 等待 22 秒 |
offset | 最多 1,000,000 |
| 路徑長度 | 4096 個字元 |
ReadFile | 512 KiB |
| DSM 7.1 上的下載 | 沒有 Content-Disposition;內容為 DSM 回應(含 success 的 JSON)且不超過 4 KiB 的檔案會被拒絕 |
| 覆寫、上層資料夾不存在 | 不支援 |
| 代碼 | 原因 | 處理方式 |
|---|---|---|
session_required | 沒有有效的 DSM 工作階段,或屬於其他使用者 | 重新登入 |
dsm_session_required | 獨立登入,沒有 DSM id cookie | 在相同主機登入 |
denied | File Station 拒絕(DSM 105、403、407、408) | 沒有權限 |
upload_size_required | 沒有 Content-Length,或超過 16 GiB | 傳送單一且小於 16 GiB 的檔案 |
commit-uncertain | 上傳或建立資料夾可能失敗 | 先檢查資料夾 |
limit-exceeded | 處理常式讀取超過 512 KiB | 減少讀取量或改用串流 |
unknown_method | 建置時未包含 files 能力 | 執行 synopkgland add files |
admin_required, app_privilege_denied | permissions.json 或 entry 被拒絕 | 請見原生 App |