Use an existing function rather than creating a new one

This commit is contained in:
Kemal Zebari 2024-04-30 01:16:30 -07:00
parent ac0df17ef1
commit e954d32d60
6 changed files with 15 additions and 29 deletions

View File

@ -178,7 +178,7 @@ func CreateIssueAttachment(ctx *context.APIContext) {
attachmentName = query
}
attachment, err := attachment.UploadAttachmentWithCustomizableName(ctx, file, setting.Attachment.AllowedTypes, header.Size, header.Filename, &repo_model.Attachment{
attachment, err := attachment.UploadAttachment(ctx, file, setting.Attachment.AllowedTypes, header.Size, header.Filename, &repo_model.Attachment{
Name: attachmentName,
UploaderID: ctx.Doer.ID,
RepoID: ctx.Repo.Repository.ID,

View File

@ -181,13 +181,13 @@ func CreateIssueCommentAttachment(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.UploadAttachment(ctx, file, setting.Attachment.AllowedTypes, header.Size, header.Filename, &repo_model.Attachment{
Name: attachmentName,
UploaderID: ctx.Doer.ID,
RepoID: ctx.Repo.Repository.ID,
IssueID: comment.IssueID,

View File

@ -206,7 +206,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
// Get uploaded file from request
var content io.ReadCloser
var filename string
var attachmentName string
var size int64 = -1
if strings.HasPrefix(strings.ToLower(ctx.Req.Header.Get("Content-Type")), "multipart/form-data") {
@ -219,23 +219,23 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
content = file
size = header.Size
filename = header.Filename
attachmentName = header.Filename
if name := ctx.FormString("name"); name != "" {
filename = name
attachmentName = name
}
} else {
content = ctx.Req.Body
filename = ctx.FormString("name")
attachmentName = ctx.FormString("name")
}
if filename == "" {
if attachmentName == "" {
ctx.Error(http.StatusBadRequest, "CreateReleaseAttachment", "Could not determine name of attachment.")
return
}
// Create a new attachment and save the file
attach, err := attachment.UploadAttachment(ctx, content, setting.Repository.Release.AllowedTypes, size, &repo_model.Attachment{
Name: filename,
attach, err := attachment.UploadAttachment(ctx, content, setting.Repository.Release.AllowedTypes, size, attachmentName, &repo_model.Attachment{
Name: attachmentName,
UploaderID: ctx.Doer.ID,
RepoID: ctx.Repo.Repository.ID,
ReleaseID: releaseID,

View File

@ -45,7 +45,7 @@ func uploadAttachment(ctx *context.Context, repoID int64, allowedTypes string) {
}
defer file.Close()
attach, err := attachment.UploadAttachment(ctx, file, allowedTypes, header.Size, &repo_model.Attachment{
attach, err := attachment.UploadAttachment(ctx, file, allowedTypes, header.Size, header.Filename, &repo_model.Attachment{
Name: header.Filename,
UploaderID: ctx.Doer.ID,
RepoID: repoID,

View File

@ -39,21 +39,7 @@ func NewAttachment(ctx context.Context, attach *repo_model.Attachment, file io.R
}
// UploadAttachment upload new attachment into storage and update database
func UploadAttachment(ctx context.Context, file io.Reader, allowedTypes string, fileSize int64, attach *repo_model.Attachment) (*repo_model.Attachment, error) {
buf := make([]byte, 1024)
n, _ := util.ReadAtMost(file, buf)
buf = buf[:n]
if err := upload.Verify(buf, attach.Name, allowedTypes); err != nil {
return nil, err
}
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) {
func UploadAttachment(ctx context.Context, file io.Reader, allowedTypes string, fileSize int64, originalFileName string, attach *repo_model.Attachment) (*repo_model.Attachment, error) {
buf := make([]byte, 1024)
n, _ := util.ReadAtMost(file, buf)
buf = buf[:n]

View File

@ -87,7 +87,7 @@ func (h *ReplyHandler) Handle(ctx context.Context, content *MailContent, doer *u
attachmentIDs := make([]string, 0, len(content.Attachments))
if setting.Attachment.Enabled {
for _, attachment := range content.Attachments {
a, err := attachment_service.UploadAttachment(ctx, bytes.NewReader(attachment.Content), setting.Attachment.AllowedTypes, int64(len(attachment.Content)), &repo_model.Attachment{
a, err := attachment_service.UploadAttachment(ctx, bytes.NewReader(attachment.Content), setting.Attachment.AllowedTypes, int64(len(attachment.Content)), attachment.Name, &repo_model.Attachment{
Name: attachment.Name,
UploaderID: doer.ID,
RepoID: issue.Repo.ID,