── File saving ── SaveResponse writes an API response body to the given outputPath and returns metadata. It delegates to FileIO.Save for path validation and atomic write; fio must not be nil.
(fio fileio.FileIO, resp *larkcore.ApiResp, outputPath string)
| 208 | // SaveResponse writes an API response body to the given outputPath and returns metadata. |
| 209 | // It delegates to FileIO.Save for path validation and atomic write; fio must not be nil. |
| 210 | func SaveResponse(fio fileio.FileIO, resp *larkcore.ApiResp, outputPath string) (map[string]interface{}, error) { |
| 211 | result, err := fio.Save(outputPath, fileio.SaveOptions{ |
| 212 | ContentType: resp.Header.Get("Content-Type"), |
| 213 | ContentLength: int64(len(resp.RawBody)), |
| 214 | }, bytes.NewReader(resp.RawBody)) |
| 215 | if err != nil { |
| 216 | var me *fileio.MkdirError |
| 217 | var we *fileio.WriteError |
| 218 | switch { |
| 219 | case errors.Is(err, fileio.ErrPathValidation): |
| 220 | return nil, fmt.Errorf("unsafe output path: %w", err) |
| 221 | case errors.As(err, &me): |
| 222 | return nil, fmt.Errorf("create directory: %w", err) |
| 223 | case errors.As(err, &we): |
| 224 | return nil, fmt.Errorf("cannot write file: %w", err) |
| 225 | default: |
| 226 | return nil, fmt.Errorf("cannot write file: %w", err) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | resolvedPath, err := fio.ResolvePath(outputPath) |
| 231 | if err != nil || resolvedPath == "" { |
| 232 | resolvedPath = outputPath |
| 233 | } |
| 234 | return map[string]interface{}{ |
| 235 | "saved_path": resolvedPath, |
| 236 | "size_bytes": result.Size(), |
| 237 | "content_type": resp.Header.Get("Content-Type"), |
| 238 | }, nil |
| 239 | } |
| 240 | |
| 241 | // ResolveFilename picks a filename from the response headers. |
| 242 | // Priority: Content-Disposition filename > Content-Type extension > "download.bin". |