- Remove duplicate UploadBaseURL constant (identical to APIBaseURL)
- Pass parentFileID and customDomain into service constructors instead of
reading from config.GlobalConfig at call time, eliminating hidden global
state dependencies in the service layer
- Replace []interface{} response building in HandleList with a typed
imageResponse struct for compile-time safety
- Extract inline CORS closure from main.go into handler.CORSMiddleware(),
consistent with how AuthMiddleware is organized
- Remove narrating comments throughout; keep only the non-obvious one
explaining why DoRawPUT omits auth headers
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
45 lines
943 B
Go
45 lines
943 B
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"imagehost/internal/pan123"
|
|
)
|
|
|
|
type ImageService struct {
|
|
client *pan123.Client
|
|
parentFileID string
|
|
customDomain string
|
|
}
|
|
|
|
func NewImageService(client *pan123.Client, parentFileID, customDomain string) *ImageService {
|
|
return &ImageService{
|
|
client: client,
|
|
parentFileID: parentFileID,
|
|
customDomain: strings.TrimRight(customDomain, "/"),
|
|
}
|
|
}
|
|
|
|
func (s *ImageService) GetImageItems() ([]pan123.FileItem, error) {
|
|
items, _, err := s.client.GetFileList(s.parentFileID, 100, "")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("获取列表失败: %w", err)
|
|
}
|
|
|
|
for i := range items {
|
|
if items[i].UserSelfURL == "" && s.customDomain != "" {
|
|
items[i].UserSelfURL = fmt.Sprintf("%s/%s", s.customDomain, items[i].Filename)
|
|
}
|
|
}
|
|
|
|
return items, nil
|
|
}
|
|
|
|
func (s *ImageService) DeleteImages(ids []string) error {
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
return s.client.DeleteFiles(ids)
|
|
}
|