mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-09-29 14:15:55 +00:00
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/7749 This adds pasted images to the dropzone. To provide the same experience as when using the dropzone. This gives the possibility to preview and delete the image. Additionally it provides a copy button to copy the markdown code for inserting the image. Fixes #4588 Co-authored-by: Beowulf <beowulf@beocode.eu> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8362 Reviewed-by: 0ko <0ko@noreply.codeberg.org> Reviewed-by: Beowulf <beowulf@beocode.eu> Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org> Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
70 lines
2.4 KiB
Go
70 lines
2.4 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package convert
|
|
|
|
import (
|
|
"mime"
|
|
"path/filepath"
|
|
|
|
repo_model "forgejo.org/models/repo"
|
|
api "forgejo.org/modules/structs"
|
|
)
|
|
|
|
func WebAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
|
|
if attach.ExternalURL != "" {
|
|
return attach.ExternalURL
|
|
}
|
|
|
|
return attach.DownloadURL()
|
|
}
|
|
|
|
func APIAssetDownloadURL(repo *repo_model.Repository, attach *repo_model.Attachment) string {
|
|
return attach.DownloadURL()
|
|
}
|
|
|
|
// ToWebAttachment converts models.Attachment to api.WebAttachment for API usage
|
|
func ToWebAttachment(repo *repo_model.Repository, a *repo_model.Attachment) *api.WebAttachment {
|
|
attachment := toAttachment(repo, a, WebAssetDownloadURL)
|
|
return &api.WebAttachment{
|
|
Attachment: attachment,
|
|
MimeType: mime.TypeByExtension(filepath.Ext(attachment.Name)),
|
|
}
|
|
}
|
|
|
|
// ToAPIAttachment converts models.Attachment to api.Attachment for API usage
|
|
func ToAPIAttachment(repo *repo_model.Repository, a *repo_model.Attachment) *api.Attachment {
|
|
return toAttachment(repo, a, APIAssetDownloadURL)
|
|
}
|
|
|
|
// toAttachment converts models.Attachment to api.Attachment for API usage
|
|
func toAttachment(repo *repo_model.Repository, a *repo_model.Attachment, getDownloadURL func(repo *repo_model.Repository, attach *repo_model.Attachment) string) *api.Attachment {
|
|
var typeName string
|
|
if a.ExternalURL != "" {
|
|
typeName = "external"
|
|
} else {
|
|
typeName = "attachment"
|
|
}
|
|
return &api.Attachment{
|
|
ID: a.ID,
|
|
Name: a.Name,
|
|
Created: a.CreatedUnix.AsTime(),
|
|
DownloadCount: a.DownloadCount,
|
|
Size: a.Size,
|
|
UUID: a.UUID,
|
|
DownloadURL: getDownloadURL(repo, a), // for web request json and api request json, return different download urls
|
|
Type: typeName,
|
|
}
|
|
}
|
|
|
|
func ToAPIAttachments(repo *repo_model.Repository, attachments []*repo_model.Attachment) []*api.Attachment {
|
|
return toAttachments(repo, attachments, APIAssetDownloadURL)
|
|
}
|
|
|
|
func toAttachments(repo *repo_model.Repository, attachments []*repo_model.Attachment, getDownloadURL func(repo *repo_model.Repository, attach *repo_model.Attachment) string) []*api.Attachment {
|
|
converted := make([]*api.Attachment, 0, len(attachments))
|
|
for _, attachment := range attachments {
|
|
converted = append(converted, toAttachment(repo, attachment, getDownloadURL))
|
|
}
|
|
return converted
|
|
}
|