add media_type column to media table in database
This commit is contained in:
parent
0eb0319fa8
commit
9e5480188b
|
@ -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_id photo_id int NOT NULL;
|
||||||
ALTER TABLE photo_url CHANGE COLUMN media_name photo_name varchar(512) 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 share_token CHANGE COLUMN media_id photo_id int;
|
||||||
|
|
||||||
|
ALTER TABLE photo DROP COLUMN media_type;
|
||||||
|
|
|
@ -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_id media_id int NOT NULL;
|
||||||
ALTER TABLE media_url CHANGE COLUMN photo_name media_name varchar(512) 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 share_token CHANGE COLUMN photo_id media_id int;
|
||||||
|
|
||||||
|
ALTER TABLE media ADD COLUMN media_type varchar(64) NOT NULL DEFAULT "photo";
|
||||||
|
|
|
@ -7,6 +7,13 @@ import (
|
||||||
"github.com/viktorstrate/photoview/api/utils"
|
"github.com/viktorstrate/photoview/api/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MediaType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
MediaTypePhoto MediaType = "photo"
|
||||||
|
MediaTypeVide MediaType = "video"
|
||||||
|
)
|
||||||
|
|
||||||
type Media struct {
|
type Media struct {
|
||||||
MediaID int
|
MediaID int
|
||||||
Title string
|
Title string
|
||||||
|
@ -15,6 +22,7 @@ type Media struct {
|
||||||
AlbumId int
|
AlbumId int
|
||||||
ExifId *int
|
ExifId *int
|
||||||
Favorite bool
|
Favorite bool
|
||||||
|
Type MediaType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Media) ID() int {
|
func (p *Media) ID() int {
|
||||||
|
@ -44,7 +52,7 @@ type MediaURL struct {
|
||||||
func NewMediaFromRow(row *sql.Row) (*Media, error) {
|
func NewMediaFromRow(row *sql.Row) (*Media, error) {
|
||||||
media := Media{}
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,22 +54,31 @@ func (c *AlbumScannerCache) AlbumContainsPhotos(path string) *bool {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *AlbumScannerCache) InsertPhotoType(path string, content_type MediaType) {
|
// func (c *AlbumScannerCache) InsertPhotoType(path string, content_type MediaType) {
|
||||||
c.mutex.Lock()
|
// c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
// 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()
|
c.mutex.Lock()
|
||||||
defer c.mutex.Unlock()
|
defer c.mutex.Unlock()
|
||||||
|
|
||||||
result, found := c.photo_types[path]
|
result, found := c.photo_types[path]
|
||||||
if found {
|
if found {
|
||||||
// log.Printf("Image cache hit: %s\n", path)
|
// 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ func (img *EncodeMediaData) ContentType() (*MediaType, error) {
|
||||||
return img._contentType, nil
|
return img._contentType, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
imgType, err := getImageType(img.media.Path)
|
imgType, err := getMediaType(img.media.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -222,7 +222,7 @@ func (imgType *MediaType) isSupported() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func getImageType(path string) (*MediaType, error) {
|
func getMediaType(path string) (*MediaType, error) {
|
||||||
|
|
||||||
ext := filepath.Ext(path)
|
ext := filepath.Ext(path)
|
||||||
|
|
||||||
|
@ -261,28 +261,23 @@ func getImageType(path string) (*MediaType, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isPathImage(path string, cache *AlbumScannerCache) bool {
|
func isPathMedia(path string, cache *AlbumScannerCache) bool {
|
||||||
if cache.GetPhotoType(path) != nil {
|
mediaType, err := cache.GetMediaType(path)
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
imageType, err := getImageType(path)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ScannerError("%s (%s)", err, path)
|
ScannerError("%s (%s)", err, path)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if imageType != nil {
|
if mediaType != nil {
|
||||||
// Make sure file isn't empty
|
// Make sure file isn't empty
|
||||||
fileStats, err := os.Stat(path)
|
fileStats, err := os.Stat(path)
|
||||||
if err != nil || fileStats.Size() == 0 {
|
if err != nil || fileStats.Size() == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.InsertPhotoType(path, *imageType)
|
|
||||||
return true
|
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
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ func scanAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.DB) {
|
||||||
notifyThrottle.Trigger(nil)
|
notifyThrottle.Trigger(nil)
|
||||||
|
|
||||||
// Scan for photos
|
// 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 {
|
if newPhoto {
|
||||||
notifyThrottle.Trigger(func() {
|
notifyThrottle.Trigger(func() {
|
||||||
notification.BroadcastNotification(&models.Notification{
|
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)
|
albumPhotos := make([]*models.Media, 0)
|
||||||
|
|
||||||
|
@ -92,14 +92,14 @@ func findPhotosForAlbum(album *models.Album, cache *AlbumScannerCache, db *sql.D
|
||||||
for _, item := range dirContent {
|
for _, item := range dirContent {
|
||||||
photoPath := path.Join(album.Path, item.Name())
|
photoPath := path.Join(album.Path, item.Name())
|
||||||
|
|
||||||
if !item.IsDir() && isPathImage(photoPath, cache) {
|
if !item.IsDir() && isPathMedia(photoPath, cache) {
|
||||||
tx, err := db.Begin()
|
tx, err := db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ScannerError("Could not begin database transaction for image %s: %s\n", photoPath, err)
|
ScannerError("Could not begin database transaction for image %s: %s\n", photoPath, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
photo, isNewPhoto, err := ScanMedia(tx, photoPath, album.AlbumID)
|
photo, isNewPhoto, err := ScanMedia(tx, photoPath, album.AlbumID, cache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ScannerError("Scanning media error (%s): %s", photoPath, err)
|
ScannerError("Scanning media error (%s): %s", photoPath, err)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
"github.com/viktorstrate/photoview/api/graphql/models"
|
"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)
|
mediaName := path.Base(mediaPath)
|
||||||
|
|
||||||
// Check if image already exists
|
// 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)
|
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 {
|
if err != nil {
|
||||||
return nil, false, errors.Wrap(err, "could not insert media into database")
|
return nil, false, errors.Wrap(err, "could not insert media into database")
|
||||||
}
|
}
|
|
@ -142,7 +142,7 @@ func directoryContainsPhotos(rootPath string, cache *AlbumScannerCache) bool {
|
||||||
if fileInfo.IsDir() {
|
if fileInfo.IsDir() {
|
||||||
scanQueue.PushBack(filePath)
|
scanQueue.PushBack(filePath)
|
||||||
} else {
|
} else {
|
||||||
if isPathImage(filePath, cache) {
|
if isPathMedia(filePath, cache) {
|
||||||
cache.InsertAlbumPaths(dirPath, rootPath, true)
|
cache.InsertAlbumPaths(dirPath, rootPath, true)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue