-
Notifications
You must be signed in to change notification settings - Fork 9k
--attach stack 5/8: Add the flag and upload behind it
#14181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package attachments | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| ) | ||
|
|
||
| // UploadAndAttach uploads every asset, points the references the markdown | ||
| // already wrote at their asset URLs, and appends the rest. It reports how many | ||
| // assets reached the server. | ||
| // | ||
| // That count is the only reason to write markdown alongside an error. An upload | ||
| // cannot be undone and there is no endpoint to delete one, so markdown that does | ||
| // not reference an uploaded asset orphans it for good. A count of zero means | ||
| // nothing was uploaded and nothing is lost by writing nothing. | ||
| // | ||
| // The markdown is returned unchanged when it could not be rewritten, so a | ||
| // caller that assigns the result in place never destroys what it was given. | ||
| func (u *Uploader) UploadAndAttach(ctx context.Context, md string, assets []UserAsset) (string, int, error) { | ||
| refs := make([]attachmentArg, len(assets)) | ||
|
BagToad marked this conversation as resolved.
|
||
| for i, a := range assets { | ||
| f := a.file() | ||
| refs[i] = attachmentArg{Path: f.path, Alt: f.alt, RendersAsPlayer: a.rendersAsPlayer()} | ||
| } | ||
|
|
||
| attachableMD, err := newAttachableMarkdown(md, refs) | ||
| if err != nil { | ||
| return md, 0, err | ||
| } | ||
|
|
||
| var failures []error | ||
| uploaded := 0 | ||
| for i, a := range assets { | ||
| assetURL, err := u.upload(ctx, a) | ||
| if err != nil { | ||
| // Stopping at the first failure. It makes recovery much simpler. | ||
| failures = append(failures, err) | ||
| break | ||
| } | ||
| refs[i].URL = assetURL | ||
| uploaded++ | ||
| } | ||
|
|
||
| attachedMD, err := attachAssetsToMarkdown(attachableMD) | ||
| if err != nil { | ||
| failures = append(failures, err) | ||
| return md, uploaded, errors.Join(failures...) | ||
| } | ||
|
|
||
| return appendUnreferenced(attachedMD, assets), uploaded, errors.Join(failures...) | ||
| } | ||
|
|
||
| // appendUnreferenced adds a paragraph for every attachment the author never | ||
| // referenced, in the order they were attached. Each one renders itself, since | ||
| // an image appends a markdown embed and a video appends a bare URL so that it | ||
| // plays, which is why this half does not live with the rewriting. | ||
| func appendUnreferenced(attachedMD attachedMarkdown, assets []UserAsset) string { | ||
| urlByPath := make(map[string]string, len(attachedMD.ToAppend)) | ||
| for _, arg := range attachedMD.ToAppend { | ||
| urlByPath[arg.Path] = arg.URL | ||
| } | ||
|
|
||
| out := attachedMD.Rewritten | ||
| for _, a := range assets { | ||
| url, ok := urlByPath[a.Path()] | ||
| if !ok { | ||
| continue | ||
| } | ||
| out = appendParagraph(out, a.markdown(url)) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // appendParagraph joins two pieces of markdown as separate paragraphs. | ||
| func appendParagraph(md, addition string) string { | ||
| md = strings.TrimRight(md, " \t\r\n") | ||
| if md == "" { | ||
| return addition | ||
| } | ||
| if addition == "" { | ||
| return md | ||
| } | ||
|
BagToad marked this conversation as resolved.
|
||
| return md + "\n\n" + addition | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.