UploadAndAttachDocuments attaches one set of files to several documents at once. It uploads each file once, in order, and stops at the first failure. Every document that references a file gets that file's URL. Each document's references resolve against its own Dir first and the working directory sec
(ctx context.Context, docs []Document, assets []UserAsset)
| 115 | // UploadAndAttach gives. One whose Err is set and whose Uploaded is empty had |
| 116 | // every attachment fail or go untried, and loses nothing by not being written. |
| 117 | func (u *Uploader) UploadAndAttachDocuments(ctx context.Context, docs []Document, assets []UserAsset) ([]DocumentResult, error) { |
| 118 | attachable, err := newAttachableDocuments(docs, assets) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | |
| 123 | urls := make([]string, len(assets)) |
| 124 | var uploadErr error |
| 125 | for i, a := range assets { |
| 126 | assetURL, err := u.upload(ctx, a) |
| 127 | if err != nil { |
| 128 | // Stopping at the first failure. It makes recovery much simpler. |
| 129 | uploadErr = err |
| 130 | break |
| 131 | } |
| 132 | urls[i] = assetURL |
| 133 | } |
| 134 | |
| 135 | failures := make([]error, 0, len(attachable)+1) |
| 136 | failures = append(failures, uploadErr) |
| 137 | results := make([]DocumentResult, len(attachable)) |
| 138 | for i, d := range attachable { |
| 139 | result, rewriteErr := d.attach(assets, urls, uploadErr) |
| 140 | results[i] = result |
| 141 | failures = append(failures, rewriteErr) |
| 142 | } |
| 143 | return results, errors.Join(failures...) |
| 144 | } |
| 145 | |
| 146 | // attachableDocument is one document scanned for the files it references, |
| 147 | // and which of the attached files are its attachments. |