1
Fork 0

Add date information to media

This closes #61
This commit is contained in:
viktorstrate 2020-08-12 13:04:41 +02:00
parent cdb4089f19
commit 78b415871d
4 changed files with 26 additions and 4 deletions

View File

@ -58,7 +58,7 @@ CREATE TABLE IF NOT EXISTS media_exif (
camera varchar(256),
maker varchar(256),
lens varchar(256),
dateShot timestamp NULL,
date_shot timestamp NULL,
exposure varchar(256),
aperture float,
iso int(6),
@ -79,6 +79,8 @@ CREATE TABLE IF NOT EXISTS media (
path_hash varchar(32) NOT NULL UNIQUE,
album_id int NOT NULL,
exif_id int,
date_shot datetime NOT NULL,
date_imported datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
favorite boolean DEFAULT FALSE,
media_type varchar(64) NOT NULL,
video_metadata_id int,

View File

@ -3,6 +3,7 @@ package models
import (
"database/sql"
"path"
"time"
"github.com/viktorstrate/photoview/api/utils"
)
@ -14,6 +15,8 @@ type Media struct {
PathHash string
AlbumId int
ExifId *int
DateShot time.Time
DateImported time.Time
Favorite bool
Type MediaType
VideoMetadataId *int
@ -47,7 +50,7 @@ type MediaURL struct {
func NewMediaFromRow(row *sql.Row) (*Media, error) {
media := Media{}
if err := row.Scan(&media.MediaID, &media.Title, &media.Path, &media.PathHash, &media.AlbumId, &media.ExifId, &media.Favorite, &media.Type, &media.VideoMetadataId); err != nil {
if err := row.Scan(&media.MediaID, &media.Title, &media.Path, &media.PathHash, &media.AlbumId, &media.ExifId, &media.DateShot, &media.DateImported, &media.Favorite, &media.Type, &media.VideoMetadataId); err != nil {
return nil, err
}

View File

@ -77,8 +77,13 @@ func ScanEXIF(tx *sql.Tx, media *models.Media) (returnExif *models.MediaEXIF, re
date, err := exifTags.DateTime()
if err == nil {
valueNames = append(valueNames, "dateShot")
valueNames = append(valueNames, "date_shot")
exifValues = append(exifValues, date)
_, err := tx.Exec("UPDATE media SET date_shot = ? WHERE media_id = ?", date, media.MediaID)
if err != nil {
log.Printf("WARN: Failed to update date_shot for media %s: %s", media.Title, err)
}
}
exposure, err := readRationalTag(exifTags, exif.ExposureTime, media)

View File

@ -3,7 +3,10 @@ package scanner
import (
"database/sql"
"log"
"os"
"path"
"syscall"
"time"
"github.com/pkg/errors"
"github.com/viktorstrate/photoview/api/graphql/models"
@ -40,7 +43,16 @@ func ScanMedia(tx *sql.Tx, mediaPath string, albumId int, cache *AlbumScannerCac
mediaTypeText = "photo"
}
result, err := tx.Exec("INSERT INTO media (title, path, path_hash, album_id, media_type) VALUES (?, ?, MD5(path), ?, ?)", mediaName, mediaPath, albumId, mediaTypeText)
stat, err := os.Stat(mediaPath)
if err != nil {
return nil, false, err
}
// Get the file creation date
sec, nsec := stat.Sys().(*syscall.Stat_t).Ctimespec.Unix()
createDate := time.Unix(sec, nsec)
result, err := tx.Exec("INSERT INTO media (title, path, path_hash, album_id, media_type, date_shot) VALUES (?, ?, MD5(path), ?, ?, ?)", mediaName, mediaPath, albumId, mediaTypeText, createDate)
if err != nil {
return nil, false, errors.Wrap(err, "could not insert media into database")
}