Skip to content
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

Properly handle the name query param in create issue attachment API #30778

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions routers/api/v1/repo/issue_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ func CreateIssueAttachment(ctx *context.APIContext) {
}
defer file.Close()

filename := header.Filename
attachmentName := header.Filename
if query := ctx.FormString("name"); query != "" {
filename = query
attachmentName = query
}

attachment, err := attachment.UploadAttachment(ctx, file, setting.Attachment.AllowedTypes, header.Size, &repo_model.Attachment{
Name: filename,
attachment, err := attachment.UploadAttachmentWithCustomizableName(ctx, file, setting.Attachment.AllowedTypes, header.Size, header.Filename, &repo_model.Attachment{
Name: attachmentName,
UploaderID: ctx.Doer.ID,
RepoID: ctx.Repo.Repository.ID,
IssueID: issue.ID,
Expand Down
14 changes: 14 additions & 0 deletions services/attachment/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@ func UploadAttachment(ctx context.Context, file io.Reader, allowedTypes string,

return NewAttachment(ctx, attach, io.MultiReader(bytes.NewReader(buf), file), fileSize)
}

// UploadAttachmentWithCustomizableName is like UploadAttachment, but you pass both the original file name and the attachment name
// (which could be the same).
func UploadAttachmentWithCustomizableName(ctx context.Context, file io.Reader, allowedTypes string, fileSize int64, originalFileName string, attach *repo_model.Attachment) (*repo_model.Attachment, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has almost the exact same definition as UploadAttachment(). I would need to tweak a couple areas if I were to change its signature so if that's alright to do let me know!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should change UploadAttachment and fix all the places but not only this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed UploadAttachment() and updated the affected areas 👍

buf := make([]byte, 1024)
n, _ := util.ReadAtMost(file, buf)
buf = buf[:n]

if err := upload.Verify(buf, originalFileName, allowedTypes); err != nil {
return nil, err
}

return NewAttachment(ctx, attach, io.MultiReader(bytes.NewReader(buf), file), fileSize)
}