1
Fork 0

add media_type column to media table in database

This commit is contained in:
viktorstrate 2020-07-11 14:05:06 +02:00
parent 0eb0319fa8
commit 9e5480188b
9 changed files with 55 additions and 27 deletions

View File

@ -7,3 +7,5 @@ ALTER TABLE photo CHANGE COLUMN media_id photo_id int NOT NULL AUTO_INCREMENT;
ALTER TABLE photo_url CHANGE COLUMN media_id photo_id int NOT NULL;
ALTER TABLE photo_url CHANGE COLUMN media_name photo_name varchar(512) NOT NULL;
ALTER TABLE share_token CHANGE COLUMN media_id photo_id int;
ALTER TABLE photo DROP COLUMN media_type;

View File

@ -7,3 +7,5 @@ ALTER TABLE media CHANGE COLUMN photo_id media_id int NOT NULL AUTO_INCREMENT;
ALTER TABLE media_url CHANGE COLUMN photo_id media_id int NOT NULL;
ALTER TABLE media_url CHANGE COLUMN photo_name media_name varchar(512) NOT NULL;
ALTER TABLE share_token CHANGE COLUMN photo_id media_id int;
ALTER TABLE media ADD COLUMN media_type varchar(64) NOT NULL DEFAULT "photo";

View File

@ -7,6 +7,13 @@ import (
"github.com/viktorstrate/photoview/api/utils"
)
type MediaType string
const (
MediaTypePhoto MediaType = "photo"
MediaTypeVide MediaType = "video"
)
type Media struct {
MediaID int
Title string
@ -15,6 +22,7 @@ type Media struct {
AlbumId int
ExifId *int
Favorite bool
Type MediaType
}
func (p *Media) ID() int {
@ -44,7 +52,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); err != nil {
if err := row.Scan(&media.MediaID, &media.Title, &media.Path, &media.PathHash, &media.AlbumId, &media.ExifId, &media.Favorite, &media.Type); err != nil {
return nil, err
}

View File

@ -54,22 +54,31 @@ func (c *AlbumScannerCache) AlbumContainsPhotos(path string) *bool {
return nil
}
func (c *AlbumScannerCache) InsertPhotoType(path string, content_type MediaType) {
c.mutex.Lock()
defer c.mutex.Unlock()
// func (c *AlbumScannerCache) InsertPhotoType(path string, content_type MediaType) {
// c.mutex.Lock()
// defer c.mutex.Unlock()
(c.photo_types)[path] = content_type
}
// (c.photo_types)[path] = content_type
// }
func (c *AlbumScannerCache) GetPhotoType(path string) *MediaType {
func (c *AlbumScannerCache) GetMediaType(path string) (*MediaType, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
result, found := c.photo_types[path]
if found {
// log.Printf("Image cache hit: %s\n", path)
return &result
return &result, nil
}
return nil
mediaType, err := getMediaType(path)
if err != nil {
return nil, err
}
if mediaType != nil {
(c.photo_types)[path] = *mediaType
}
return mediaType, nil
}

View File

@ -92,7 +92,7 @@ func (img *EncodeMediaData) ContentType() (*MediaType, error) {
return img._contentType, nil
}
imgType, err := getImageType(img.media.Path)
imgType, err := getMediaType(img.media.Path)
if err != nil {
return nil, err
}

View File

@ -222,7 +222,7 @@ func (imgType *MediaType) isSupported() bool {
return false
}
func getImageType(path string) (*MediaType, error) {
func getMediaType(path string) (*MediaType, error) {
ext := filepath.Ext(path)
@ -261,28 +261,23 @@ func getImageType(path string) (*MediaType, error) {
return nil, nil
}
func isPathImage(path string, cache *AlbumScannerCache) bool {
if cache.GetPhotoType(path) != nil {
return true
}
imageType, err := getImageType(path)
func isPathMedia(path string, cache *AlbumScannerCache) bool {
mediaType, err := cache.GetMediaType(path)
if err != nil {
ScannerError("%s (%s)", err, path)
return false
}
if imageType != nil {
if mediaType != nil {
// Make sure file isn't empty
fileStats, err := os.Stat(path)
if err != nil || fileStats.Size() == 0 {
return false
}
cache.InsertPhotoType(path, *imageType)
return true
}
log.Printf("File is not a supported image %s\n", path)
log.Printf("File is not a supported media %s\n", path)
return false
}

View File

@ -19,7 +19,7 @@ func scanAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.DB) {
notifyThrottle.Trigger(nil)
// Scan for photos
albumPhotos, err := findPhotosForAlbum(album, cache, db, func(photo *models.Media, newPhoto bool) {
albumPhotos, err := findMediaForAlbum(album, cache, db, func(photo *models.Media, newPhoto bool) {
if newPhoto {
notifyThrottle.Trigger(func() {
notification.BroadcastNotification(&models.Notification{
@ -80,7 +80,7 @@ func scanAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.DB) {
}
}
func findPhotosForAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.DB, onScanPhoto func(photo *models.Media, newPhoto bool)) ([]*models.Media, error) {
func findMediaForAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.DB, onScanPhoto func(photo *models.Media, newPhoto bool)) ([]*models.Media, error) {
albumPhotos := make([]*models.Media, 0)
@ -92,14 +92,14 @@ func findPhotosForAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.D
for _, item := range dirContent {
photoPath := path.Join(album.Path, item.Name())
if !item.IsDir() && isPathImage(photoPath, cache) {
if !item.IsDir() && isPathMedia(photoPath, cache) {
tx, err := db.Begin()
if err != nil {
ScannerError("Could not begin database transaction for image %s: %s\n", photoPath, err)
continue
}
photo, isNewPhoto, err := ScanMedia(tx, photoPath, album.AlbumID)
photo, isNewPhoto, err := ScanMedia(tx, photoPath, album.AlbumID, cache)
if err != nil {
ScannerError("Scanning media error (%s): %s", photoPath, err)
tx.Rollback()

View File

@ -9,7 +9,7 @@ import (
"github.com/viktorstrate/photoview/api/graphql/models"
)
func ScanMedia(tx *sql.Tx, mediaPath string, albumId int) (*models.Media, bool, error) {
func ScanMedia(tx *sql.Tx, mediaPath string, albumId int, cache *AlbumScannerCache) (*models.Media, bool, error) {
mediaName := path.Base(mediaPath)
// Check if image already exists
@ -28,7 +28,19 @@ func ScanMedia(tx *sql.Tx, mediaPath string, albumId int) (*models.Media, bool,
log.Printf("Scanning media: %s\n", mediaPath)
result, err := tx.Exec("INSERT INTO media (title, path, path_hash, album_id) VALUES (?, ?, MD5(path), ?)", mediaName, mediaPath, albumId)
mediaType, err := cache.GetMediaType(mediaPath)
if err != nil {
return nil, false, errors.Wrap(err, "could determine if media was photo or video")
}
var mediaTypeText string
if mediaType.isVideo() {
mediaTypeText = "video"
} else {
mediaTypeText = "photo"
}
result, err := tx.Exec("INSERT INTO media (title, path, path_hash, album_id, media_type) VALUES (?, ?, MD5(path), ?, ?)", mediaName, mediaPath, albumId, mediaTypeText)
if err != nil {
return nil, false, errors.Wrap(err, "could not insert media into database")
}

View File

@ -142,7 +142,7 @@ func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
if fileInfo.IsDir() {
scanQueue.PushBack(filePath)
} else {
if isPathImage(filePath, cache) {
if isPathMedia(filePath, cache) {
cache.InsertAlbumPaths(dirPath, rootPath, true)
return true
}