97 lines
2.9 KiB
Go
97 lines
2.9 KiB
Go
package album
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path"
|
|
"strings"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// MailImageLibraryMorph is the WinterCMS-compatible attachment_type used by
|
|
// Phase 35 Mail Images rows in system_files.
|
|
const MailImageLibraryMorph = `System\Models\MailImageLibrary`
|
|
|
|
// BackfillMailImages idempotently indexes existing system_files rows belonging
|
|
// to the Mail Image Library into summer_media_items under the virtual "mail"
|
|
// folder, keyed by legacy_file_id. Existing blobs and /uploads/... URLs are
|
|
// preserved untouched so previously sent or authored mail templates never break.
|
|
func BackfillMailImages(ctx context.Context, db *gorm.DB, cfg *compass.Config) (int, error) {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
if db == nil {
|
|
return 0, fmt.Errorf("album: db is nil")
|
|
}
|
|
|
|
var files []attach.File
|
|
err := db.WithContext(ctx).
|
|
Where("attachment_type = ? AND field = ?", MailImageLibraryMorph, "images").
|
|
Where("id NOT IN (SELECT legacy_file_id FROM "+ItemsTable+" WHERE legacy_file_id IS NOT NULL)").
|
|
Order("id ASC").
|
|
Find(&files).Error
|
|
if err != nil {
|
|
return 0, fmt.Errorf("album: query legacy mail images: %w", err)
|
|
}
|
|
if len(files) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
svc := NewService(cfg)
|
|
mailFolder, err := svc.EnsureFolder(ctx, db, "mail", "Mail", nil)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("album: ensure mail folder: %w", err)
|
|
}
|
|
|
|
inserted := 0
|
|
for _, f := range files {
|
|
shortID, err := generateUniqueShortID(ctx, db)
|
|
if err != nil {
|
|
return inserted, err
|
|
}
|
|
baseName := clientBaseName(f.FileName)
|
|
slug := SanitizeSlug(strings.TrimSuffix(baseName, path.Ext(baseName)))
|
|
blobKey := attach.BlobKey(f.DiskName)
|
|
legacyURL := attach.PublicURL(blobKey)
|
|
legacyID := f.ID
|
|
title := ""
|
|
if f.Title != nil {
|
|
title = strings.TrimSpace(*f.Title)
|
|
}
|
|
folderID := mailFolder.ID
|
|
|
|
item := &Item{
|
|
ShortID: shortID,
|
|
FolderID: &folderID,
|
|
BlobKey: blobKey,
|
|
Slug: slug,
|
|
FileName: baseName,
|
|
ContentType: f.ContentType,
|
|
FileSize: f.FileSize,
|
|
Title: title,
|
|
Variants: Variants{},
|
|
LegacyFileID: &legacyID,
|
|
LegacyURL: legacyURL,
|
|
}
|
|
res := db.WithContext(ctx).Exec(
|
|
`INSERT INTO `+ItemsTable+`
|
|
(short_id, folder_id, blob_key, slug, file_name, content_type, file_size, sha256, alt, title, caption, variants, legacy_file_id, legacy_url, created_at, updated_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, '', '', ?, '', '[]'::jsonb, ?, ?, ?, ?)
|
|
ON CONFLICT (legacy_file_id) DO NOTHING`,
|
|
item.ShortID, item.FolderID, item.BlobKey, item.Slug, item.FileName,
|
|
item.ContentType, item.FileSize, item.Title, *item.LegacyFileID, item.LegacyURL,
|
|
f.CreatedAt, f.UpdatedAt,
|
|
)
|
|
if res.Error != nil {
|
|
return inserted, fmt.Errorf("album: backfill legacy mail image %d: %w", f.ID, res.Error)
|
|
}
|
|
if res.RowsAffected > 0 {
|
|
inserted++
|
|
}
|
|
}
|
|
return inserted, nil
|
|
}
|