2016-05-17 23:03:37 -07:00
|
|
|
package arrange
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/md5"
|
2026-10-22 13:59:09 -07:00
|
|
|
"crypto/sha256"
|
2016-05-17 23:03:37 -07:00
|
|
|
"fmt"
|
2026-10-22 13:59:09 -07:00
|
|
|
"hash"
|
2016-05-18 21:11:18 -07:00
|
|
|
"image/gif"
|
2016-05-17 23:03:37 -07:00
|
|
|
"image/jpeg"
|
2016-05-18 21:11:18 -07:00
|
|
|
"image/png"
|
2016-05-17 23:03:37 -07:00
|
|
|
"io"
|
2026-10-22 13:59:09 -07:00
|
|
|
"io/fs"
|
2016-05-17 23:03:37 -07:00
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
// kind is how a file extension gets parsed.
|
|
|
|
|
type kind int
|
2016-05-18 00:19:39 -07:00
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
const (
|
|
|
|
|
kindJPEG kind = iota
|
|
|
|
|
kindPNG
|
|
|
|
|
kindGIF
|
|
|
|
|
kindStill // HEIC, camera RAW, etc.: dated by exif, no decode check
|
|
|
|
|
kindQuickTime // mov/mp4 family: dated by the mvhd atom
|
|
|
|
|
kindVideo // everything else: dated by mtime
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var exts = map[string]kind{
|
|
|
|
|
// images
|
|
|
|
|
".jpg": kindJPEG,
|
|
|
|
|
".jpeg": kindJPEG,
|
|
|
|
|
".png": kindPNG,
|
|
|
|
|
".gif": kindGIF,
|
|
|
|
|
".heic": kindStill,
|
|
|
|
|
".heif": kindStill,
|
|
|
|
|
".avif": kindStill,
|
|
|
|
|
".tif": kindStill,
|
|
|
|
|
".tiff": kindStill,
|
|
|
|
|
".dng": kindStill,
|
|
|
|
|
".cr2": kindStill,
|
|
|
|
|
".cr3": kindStill,
|
|
|
|
|
".nef": kindStill,
|
|
|
|
|
".arw": kindStill,
|
|
|
|
|
|
|
|
|
|
// videos
|
|
|
|
|
".mov": kindQuickTime,
|
|
|
|
|
".mp4": kindQuickTime,
|
|
|
|
|
".m4v": kindQuickTime,
|
|
|
|
|
".3gp": kindQuickTime,
|
|
|
|
|
".avi": kindVideo,
|
|
|
|
|
".mkv": kindVideo,
|
|
|
|
|
".mts": kindVideo,
|
|
|
|
|
".m2ts": kindVideo,
|
|
|
|
|
".mpg": kindVideo,
|
|
|
|
|
".mpeg": kindVideo,
|
|
|
|
|
".wmv": kindVideo,
|
|
|
|
|
".webm": kindVideo,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewHash makes the hash used for content addresses. Stores built by older
|
|
|
|
|
// versions used md5; set this to md5.New to keep adding to one of those.
|
|
|
|
|
var NewHash func() hash.Hash = sha256.New
|
2016-05-18 00:19:39 -07:00
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
// HashByName returns the hash constructor matching the length of a hex
|
|
|
|
|
// content address, or nil if it is not one we know.
|
|
|
|
|
func HashByName(hexname string) func() hash.Hash {
|
|
|
|
|
switch len(hexname) {
|
|
|
|
|
case 2 * md5.Size:
|
|
|
|
|
return md5.New
|
|
|
|
|
case 2 * sha256.Size:
|
|
|
|
|
return sha256.New
|
2016-05-18 00:19:39 -07:00
|
|
|
}
|
2026-10-22 13:59:09 -07:00
|
|
|
return nil
|
2016-05-18 00:19:39 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-18 21:11:18 -07:00
|
|
|
func mtime(path string) (time.Time, error) {
|
|
|
|
|
ti := time.Time{}
|
|
|
|
|
s, err := os.Stat(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ti, fmt.Errorf("failure to collect times from stat: %v", err)
|
|
|
|
|
}
|
|
|
|
|
return s.ModTime(), nil
|
2016-05-17 23:03:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:29:17 -07:00
|
|
|
// PrepOutput creates all possible content-address prefix directories.
|
2016-05-17 23:03:37 -07:00
|
|
|
func PrepOutput(root string) error {
|
|
|
|
|
for i := 0; i <= 0xff; i++ {
|
|
|
|
|
dirname := filepath.Join(root, "content", fmt.Sprintf("%02x", i))
|
|
|
|
|
if err := os.MkdirAll(dirname, 0755); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if err := os.MkdirAll(filepath.Join(root, "date"), 0755); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
// Source sends all files that match known extensions. Unreadable paths are
|
|
|
|
|
// logged and skipped rather than ending the crawl.
|
2016-05-18 00:19:39 -07:00
|
|
|
func Source(root string) <-chan string {
|
2016-05-17 23:03:37 -07:00
|
|
|
out := make(chan string)
|
|
|
|
|
go func() {
|
2026-10-22 13:59:09 -07:00
|
|
|
err := filepath.WalkDir(
|
2016-05-17 23:03:37 -07:00
|
|
|
root,
|
2026-10-22 13:59:09 -07:00
|
|
|
func(path string, d fs.DirEntry, err error) error {
|
2016-05-18 21:04:21 -07:00
|
|
|
if err != nil {
|
2026-10-22 13:59:09 -07:00
|
|
|
if path == root {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
log.Printf("skipping %q: %v", path, err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
name := d.Name()
|
|
|
|
|
if d.IsDir() {
|
|
|
|
|
// QNAP thumbnail caches
|
|
|
|
|
if name == ".@__thumb" {
|
|
|
|
|
return filepath.SkipDir
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2016-05-18 21:04:21 -07:00
|
|
|
}
|
2026-10-22 13:59:09 -07:00
|
|
|
// AppleDouble resource forks
|
|
|
|
|
if strings.HasPrefix(name, "._") {
|
2016-05-17 23:03:37 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
2026-10-22 13:59:09 -07:00
|
|
|
if _, ok := exts[strings.ToLower(filepath.Ext(path))]; ok {
|
2016-05-17 23:03:37 -07:00
|
|
|
out <- path
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("problem during crawl: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
close(out)
|
|
|
|
|
}()
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:29:17 -07:00
|
|
|
// Parse runs the file parser for each file on input chan, and sends results
|
|
|
|
|
// down output chan.
|
|
|
|
|
//
|
|
|
|
|
// Exists so that it can be called many times concurrently.
|
2016-05-18 21:11:18 -07:00
|
|
|
func Parse(in <-chan string) <-chan Media {
|
|
|
|
|
out := make(chan Media)
|
2016-05-17 23:03:37 -07:00
|
|
|
go func() {
|
|
|
|
|
for path := range in {
|
2017-07-23 14:06:28 -07:00
|
|
|
f, err := ParseFile(path)
|
2016-05-17 23:03:37 -07:00
|
|
|
if err != nil {
|
|
|
|
|
switch err.(type) {
|
|
|
|
|
case NotMedia:
|
|
|
|
|
log.Printf("%+v", err)
|
|
|
|
|
default:
|
|
|
|
|
log.Printf("parse error: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
} else {
|
|
|
|
|
out <- f
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
close(out)
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-09 21:40:14 -07:00
|
|
|
// MissingLink detects if the values coming from medias is a duplicate file
|
2026-10-22 13:59:09 -07:00
|
|
|
// rather than a hardlink to the content store. Files whose content is not in
|
|
|
|
|
// the store are reported on errs and never sent on, since there is nothing to
|
|
|
|
|
// link them to.
|
2016-07-09 21:40:14 -07:00
|
|
|
func MissingLink(medias <-chan Media, root string) (<-chan Media, <-chan error) {
|
|
|
|
|
out := make(chan Media)
|
|
|
|
|
errs := make(chan error)
|
|
|
|
|
go func() {
|
|
|
|
|
for m := range medias {
|
2026-10-22 13:59:09 -07:00
|
|
|
d, err := os.Stat(m.Path)
|
|
|
|
|
if err != nil {
|
2016-07-09 21:40:14 -07:00
|
|
|
errs <- err
|
2026-10-22 13:59:09 -07:00
|
|
|
continue
|
2016-07-09 21:40:14 -07:00
|
|
|
}
|
2026-10-22 13:59:09 -07:00
|
|
|
c, err := os.Stat(m.Content(root))
|
|
|
|
|
if err != nil {
|
|
|
|
|
errs <- fmt.Errorf("%q has no content in store: %v", m.Path, err)
|
|
|
|
|
continue
|
2016-07-09 21:40:14 -07:00
|
|
|
}
|
|
|
|
|
if !os.SameFile(d, c) {
|
|
|
|
|
out <- m
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
close(errs)
|
|
|
|
|
close(out)
|
|
|
|
|
}()
|
|
|
|
|
return out, errs
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
// Relink atomically replaces m.Path with a hardlink to its content in the
|
|
|
|
|
// store, so m.Path exists at every moment.
|
|
|
|
|
func Relink(m Media, root string) error {
|
|
|
|
|
content := m.Content(root)
|
|
|
|
|
if _, err := os.Stat(content); err != nil {
|
|
|
|
|
return fmt.Errorf("refusing to relink %q: %v", m.Path, err)
|
|
|
|
|
}
|
|
|
|
|
tmp := filepath.Join(filepath.Dir(m.Path), fmt.Sprintf(".arrange-%d-%s", os.Getpid(), filepath.Base(m.Path)))
|
|
|
|
|
if err := os.Link(content, tmp); err != nil {
|
|
|
|
|
return fmt.Errorf("couldn't link %q: %v", tmp, err)
|
|
|
|
|
}
|
|
|
|
|
if err := os.Rename(tmp, m.Path); err != nil {
|
|
|
|
|
os.Remove(tmp)
|
|
|
|
|
return fmt.Errorf("couldn't replace %q: %v", m.Path, err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:29:17 -07:00
|
|
|
// Move calls Move on each Media on input chan. It is the first step in the
|
|
|
|
|
// pipeline after fan-in.
|
2016-05-18 21:11:18 -07:00
|
|
|
func Move(in <-chan Media, root string) <-chan error {
|
2016-05-17 23:03:37 -07:00
|
|
|
out := make(chan error)
|
|
|
|
|
go func() {
|
|
|
|
|
for i := range in {
|
|
|
|
|
out <- i.Move(root)
|
|
|
|
|
}
|
|
|
|
|
close(out)
|
|
|
|
|
}()
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-23 14:06:28 -07:00
|
|
|
// ParseFile extracts metadata from single file.
|
|
|
|
|
func ParseFile(path string) (Media, error) {
|
2016-05-17 23:03:37 -07:00
|
|
|
ext := strings.ToLower(filepath.Ext(path))
|
2016-05-18 21:11:18 -07:00
|
|
|
var r Media
|
2026-10-22 13:59:09 -07:00
|
|
|
|
|
|
|
|
k, ok := exts[ext]
|
|
|
|
|
if !ok {
|
|
|
|
|
return r, NotMedia{path}
|
|
|
|
|
}
|
2016-05-18 21:11:18 -07:00
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return r, fmt.Errorf("problem opening file: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
// make sure it is what it says it is
|
|
|
|
|
switch k {
|
|
|
|
|
case kindJPEG:
|
|
|
|
|
_, err = jpeg.DecodeConfig(f)
|
|
|
|
|
case kindPNG:
|
|
|
|
|
_, err = png.DecodeConfig(f)
|
|
|
|
|
case kindGIF:
|
|
|
|
|
_, err = gif.DecodeConfig(f)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2016-05-18 21:11:18 -07:00
|
|
|
return r, NotMedia{path}
|
2026-10-22 13:59:09 -07:00
|
|
|
}
|
2016-05-17 23:03:37 -07:00
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
// try a few things for a time value, falling back to mtime
|
|
|
|
|
var t time.Time
|
|
|
|
|
switch k {
|
|
|
|
|
case kindJPEG, kindPNG, kindStill:
|
|
|
|
|
t = exifTime(f, k == kindJPEG)
|
|
|
|
|
case kindQuickTime:
|
|
|
|
|
if _, err := f.Seek(0, io.SeekStart); err == nil {
|
|
|
|
|
if qt, err := quickTimeCreated(f); err == nil {
|
|
|
|
|
t = qt
|
2016-05-17 23:03:37 -07:00
|
|
|
}
|
|
|
|
|
}
|
2026-10-22 13:59:09 -07:00
|
|
|
}
|
|
|
|
|
if t.IsZero() {
|
|
|
|
|
if t, err = mtime(path); err != nil {
|
|
|
|
|
return r, fmt.Errorf("unable to calculate reasonable time for media %q: %v", path, err)
|
2016-05-18 21:11:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-22 13:59:09 -07:00
|
|
|
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
2016-05-18 21:11:18 -07:00
|
|
|
return r, fmt.Errorf("couldn't seek back in file: %v", err)
|
|
|
|
|
}
|
2026-10-22 13:59:09 -07:00
|
|
|
hash := NewHash()
|
|
|
|
|
n, err := io.Copy(hash, f)
|
|
|
|
|
if err != nil {
|
2016-05-18 21:11:18 -07:00
|
|
|
return r, fmt.Errorf("problem calculating checksum on %q: %v", path, err)
|
|
|
|
|
}
|
|
|
|
|
r = Media{
|
|
|
|
|
Path: path,
|
|
|
|
|
Hash: fmt.Sprintf("%x", hash.Sum(nil)),
|
|
|
|
|
Extension: ext,
|
|
|
|
|
Time: t,
|
2026-10-22 13:59:09 -07:00
|
|
|
Size: n,
|
2016-05-17 23:03:37 -07:00
|
|
|
}
|
|
|
|
|
return r, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 21:29:17 -07:00
|
|
|
// Merge implements fan-in.
|
2016-05-18 21:11:18 -07:00
|
|
|
func Merge(cs []<-chan Media) <-chan Media {
|
|
|
|
|
out := make(chan Media)
|
2016-05-17 23:03:37 -07:00
|
|
|
var wg sync.WaitGroup
|
2016-05-18 21:11:18 -07:00
|
|
|
output := func(c <-chan Media) {
|
2016-05-17 23:03:37 -07:00
|
|
|
for n := range c {
|
|
|
|
|
out <- n
|
|
|
|
|
}
|
|
|
|
|
wg.Done()
|
|
|
|
|
}
|
|
|
|
|
for _, c := range cs {
|
|
|
|
|
go output(c)
|
|
|
|
|
}
|
|
|
|
|
wg.Add(len(cs))
|
|
|
|
|
go func() {
|
|
|
|
|
wg.Wait()
|
|
|
|
|
close(out)
|
|
|
|
|
}()
|
|
|
|
|
return out
|
|
|
|
|
}
|