增加复制粘贴交互
This commit is contained in:
1 parent
4195525dc7
commit
deb9cc5bcf
14 files changed
+1144
-31
No files matched your search
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
@@ -76,6 +77,8 @@ func (h *assetsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
h.platformCollection(w, r, method)
|
||||
case "platform-upload":
|
||||
h.upload(w, r, false)
|
||||
case "platform-import-image":
|
||||
h.importImage(w, r)
|
||||
case "platform-item":
|
||||
h.delete(w, r, values[0])
|
||||
case "platform-download":
|
||||
@@ -156,6 +159,45 @@ func (h *assetsHandler) publicCollection(w http.ResponseWriter, r *http.Request,
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"asset": created})
|
||||
}
|
||||
|
||||
func (h *assetsHandler) importImage(w http.ResponseWriter, r *http.Request) {
|
||||
scope, ok := h.platformScope(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
mediaType, _, mediaErr := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if mediaErr != nil || mediaType != "application/json" {
|
||||
writeAssetJSONError(w, http.StatusUnsupportedMediaType, "请使用 JSON 请求")
|
||||
return
|
||||
}
|
||||
if r.Header.Get("Sec-Fetch-Site") == "cross-site" {
|
||||
writeAssetJSONError(w, http.StatusForbidden, "跨站请求不被允许")
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
if !decodeAssetJSON(w, r, h.config.MaxJSONBytes, &input) {
|
||||
return
|
||||
}
|
||||
created, err := h.service.ImportImage(r.Context(), scope, input.URL)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, assets.ErrImportImageURL):
|
||||
writeAssetJSONError(w, http.StatusBadRequest, "图片地址无效,请使用 http 或 https 链接")
|
||||
case errors.Is(err, assets.ErrRemoteTooLarge):
|
||||
writeAssetJSONError(w, http.StatusRequestEntityTooLarge, "图片过大")
|
||||
case errors.Is(err, assets.ErrImportImageUnsupported):
|
||||
writeAssetJSONError(w, http.StatusUnsupportedMediaType, "不支持的图片格式")
|
||||
case errors.Is(err, assets.ErrImportImageUnavailable):
|
||||
writeAssetJSONError(w, http.StatusBadGateway, "图片地址不可访问")
|
||||
default:
|
||||
writeAssetJSONError(w, http.StatusInternalServerError, "Internal server error.")
|
||||
}
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, map[string]any{"asset": created})
|
||||
}
|
||||
|
||||
type createAssetInput struct {
|
||||
URL string `json:"url"`
|
||||
Name string `json:"name"`
|
||||
@@ -456,6 +498,9 @@ func matchAssetRoute(value string) (string, []string) {
|
||||
if value == "/api/assets/upload" {
|
||||
return "platform-upload", nil
|
||||
}
|
||||
if value == "/api/assets/import-image" {
|
||||
return "platform-import-image", nil
|
||||
}
|
||||
if value == "/api/v1/assets" {
|
||||
return "public-collection", nil
|
||||
}
|
||||
@@ -491,7 +536,7 @@ func assetRouteAllow(route string) string {
|
||||
switch route {
|
||||
case "platform-collection", "public-collection":
|
||||
return "GET, HEAD, POST, OPTIONS"
|
||||
case "platform-upload":
|
||||
case "platform-upload", "platform-import-image":
|
||||
return "POST, OPTIONS"
|
||||
case "platform-item":
|
||||
return "DELETE, OPTIONS"
|
||||
|
||||
Reference in new issue
Block a user