feat(media): images and recordings in backups, exports and imports (M6)
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 1m52s
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 1m52s
- Backup ZIP (download and automatic per-user server backups) carries the files of the user's own notes as media/<id><ext>; restoring creates them with new IDs and rewrites the blocks. - Markdown archive puts them under attachments/ and embeds them relative to each note (); re-importing turns them back into image/audio blocks. - Obsidian import takes images and audio a note embeds on its own (![[file.png]], ); unused or invalid files appear as skipped in the preview instead of vanishing. - Format detection and file paths moved into storage for this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3f2bc96732
commit
1f63e08a73
13 files changed
+608
-82
No files matched your search
+119
-1
@@ -3,6 +3,10 @@ package storage
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sandrodach/mindsift/internal/domain"
|
||||
@@ -26,6 +30,72 @@ type Media struct {
|
||||
|
||||
var ErrMediaNotFound = errors.New("Datei nicht gefunden")
|
||||
|
||||
// Accepted formats (detected from the content, not from the name): file extension and kind
|
||||
var mediaTypes = map[string]struct{ ext, kind string }{
|
||||
"image/jpeg": {".jpg", "image"},
|
||||
"image/png": {".png", "image"},
|
||||
"image/gif": {".gif", "image"},
|
||||
"image/webp": {".webp", "image"},
|
||||
"audio/webm": {".webm", "audio"},
|
||||
"video/webm": {".webm", "audio"}, // Chrome's audio-only recordings are detected as video/webm
|
||||
"audio/ogg": {".ogg", "audio"},
|
||||
"application/ogg": {".ogg", "audio"},
|
||||
"audio/mpeg": {".mp3", "audio"},
|
||||
"audio/wave": {".wav", "audio"},
|
||||
"audio/wav": {".wav", "audio"},
|
||||
"audio/mp4": {".m4a", "audio"},
|
||||
"video/mp4": {".m4a", "audio"}, // Safari records MP4/AAC
|
||||
"audio/aac": {".aac", "audio"},
|
||||
}
|
||||
|
||||
// DetectMedia recognises the format from the first bytes. Browsers send audio as e.g.
|
||||
// "audio/webm;codecs=opus"; that name is kept when the content looks like a container we accept.
|
||||
func DetectMedia(data []byte, declared string) (mime, kind string, ok bool) {
|
||||
sniffed := http.DetectContentType(data)
|
||||
if i := strings.IndexByte(sniffed, ';'); i >= 0 {
|
||||
sniffed = sniffed[:i]
|
||||
}
|
||||
// Go does not know WebP and M4A by name; check their signatures directly
|
||||
switch {
|
||||
case len(data) >= 12 && string(data[0:4]) == "RIFF" && string(data[8:12]) == "WEBP":
|
||||
sniffed = "image/webp"
|
||||
case len(data) >= 12 && string(data[4:8]) == "ftyp":
|
||||
sniffed = "video/mp4"
|
||||
}
|
||||
t, ok := mediaTypes[sniffed]
|
||||
if !ok {
|
||||
return "", "", false
|
||||
}
|
||||
if t.kind == "audio" {
|
||||
if d := strings.ToLower(strings.TrimSpace(strings.SplitN(declared, ";", 2)[0])); strings.HasPrefix(d, "audio/") {
|
||||
if dt, known := mediaTypes[d]; known && dt.kind == "audio" {
|
||||
sniffed = d
|
||||
}
|
||||
} else if sniffed == "video/webm" {
|
||||
sniffed = "audio/webm"
|
||||
} else if sniffed == "video/mp4" {
|
||||
sniffed = "audio/mp4"
|
||||
}
|
||||
}
|
||||
return sniffed, t.kind, true
|
||||
}
|
||||
|
||||
// MediaExt is the file extension for a stored format.
|
||||
func MediaExt(mime string) string {
|
||||
if t, ok := mediaTypes[mime]; ok {
|
||||
return t.ext
|
||||
}
|
||||
return ".bin"
|
||||
}
|
||||
|
||||
// MediaRoot is the folder with all media files ("" for in-memory databases).
|
||||
func (s *Store) MediaRoot() string { return s.mediaRoot }
|
||||
|
||||
// MediaFilePath is where the file of m lies.
|
||||
func (s *Store) MediaFilePath(m *Media) string {
|
||||
return filepath.Join(s.mediaRoot, filepath.Base(m.DocID), filepath.Base(m.ID)+MediaExt(m.Mime))
|
||||
}
|
||||
|
||||
func (s *Store) migrateMedia() error {
|
||||
return s.execInTx(
|
||||
stmt(`CREATE TABLE IF NOT EXISTS media (
|
||||
@@ -44,7 +114,11 @@ func (s *Store) migrateMedia() error {
|
||||
}
|
||||
|
||||
func (s *Store) CreateMedia(docID, ownerID, kind, mime string, size int64, sourceURL string) (*Media, error) {
|
||||
m := &Media{ID: domain.NewID("med"), DocID: docID, OwnerID: ownerID, Kind: kind, Mime: mime, Size: size,
|
||||
return s.createMediaWithID(domain.NewID("med"), docID, ownerID, kind, mime, size, sourceURL)
|
||||
}
|
||||
|
||||
func (s *Store) createMediaWithID(id, docID, ownerID, kind, mime string, size int64, sourceURL string) (*Media, error) {
|
||||
m := &Media{ID: id, DocID: docID, OwnerID: ownerID, Kind: kind, Mime: mime, Size: size,
|
||||
SourceURL: sourceURL, CreatedAt: nowUTC()}
|
||||
_, err := s.db.Exec(`INSERT INTO media (id, doc_id, owner_id, kind, mime, size, source_url, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
m.ID, m.DocID, m.OwnerID, m.Kind, m.Mime, m.Size, m.SourceURL, m.CreatedAt)
|
||||
@@ -117,3 +191,47 @@ func (s *Store) UnreferencedMedia(olderThan time.Time) ([]Media, error) {
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// MediaOfUser lists the files shown in the user's own notes (for backups and exports).
|
||||
func (s *Store) MediaOfUser(userID string) ([]Media, error) {
|
||||
rows, err := s.db.Query(`SELECT DISTINCT m.id, m.doc_id, m.owner_id, m.kind, m.mime, m.size, m.source_url, m.created_at
|
||||
FROM media m JOIN blocks b ON instr(b.content, 'media:' || m.id) > 0
|
||||
JOIN documents d ON d.id = b.document_id WHERE d.user_id = ?`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Media
|
||||
for rows.Next() {
|
||||
var m Media
|
||||
if rows.Scan(&m.ID, &m.DocID, &m.OwnerID, &m.Kind, &m.Mime, &m.Size, &m.SourceURL, &m.CreatedAt) == nil {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// MediaRef is a block's pointer to a file:  for images, [caption](media:id) for audio.
|
||||
type MediaRef struct{ ID, Caption string }
|
||||
|
||||
var mediaRefRe = regexp.MustCompile(`^!?\[((?:[^\]\\]|\\.)*)\]\(media:(med_[0-9a-f]+)\)$`)
|
||||
var mediaRefEsc = regexp.MustCompile(`\\(.)`)
|
||||
|
||||
func ParseMediaContent(content string) (MediaRef, bool) {
|
||||
m := mediaRefRe.FindStringSubmatch(strings.TrimSpace(content))
|
||||
if m == nil {
|
||||
return MediaRef{}, false
|
||||
}
|
||||
return MediaRef{ID: m[2], Caption: mediaRefEsc.ReplaceAllString(m[1], "$1")}, true
|
||||
}
|
||||
|
||||
// MediaContent builds a block's content for a file (same format as the browser, lib/media.ts).
|
||||
func MediaContent(kind, id, caption string) string {
|
||||
caption = strings.Join(strings.Fields(strings.ReplaceAll(caption, "\n", " ")), " ")
|
||||
caption = strings.NewReplacer(`\`, `\\`, `]`, `\]`).Replace(caption)
|
||||
prefix := ""
|
||||
if kind == "image" {
|
||||
prefix = "!"
|
||||
}
|
||||
return prefix + "[" + caption + "](media:" + id + ")"
|
||||
}
|
||||
Reference in new issue
Block a user