Extract filetypes into its own type
This commit is contained in:
parent
59de859d50
commit
5a5c06fb32
|
@ -4,7 +4,6 @@ import (
|
|||
"container/list"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
@ -13,7 +12,6 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/h2non/filetype"
|
||||
"github.com/viktorstrate/photoview/api/graphql/models"
|
||||
"github.com/viktorstrate/photoview/api/graphql/notification"
|
||||
"github.com/viktorstrate/photoview/api/utils"
|
||||
|
@ -21,7 +19,7 @@ import (
|
|||
|
||||
type scanner_cache map[string]interface{}
|
||||
|
||||
func (cache *scanner_cache) insert_photo_type(path string, content_type string) {
|
||||
func (cache *scanner_cache) insert_photo_type(path string, content_type FileType) {
|
||||
(*cache)["photo_type//"+path] = content_type
|
||||
}
|
||||
|
||||
|
@ -311,59 +309,6 @@ func directoryContainsPhotos(rootPath string, cache *scanner_cache) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
var SupportedMimetypes = [...]string{
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/tiff",
|
||||
"image/webp",
|
||||
"image/x-canon-cr2",
|
||||
"image/bmp",
|
||||
}
|
||||
|
||||
var WebMimetypes = [...]string{
|
||||
"image/jpeg",
|
||||
"image/png",
|
||||
"image/webp",
|
||||
"image/bmp",
|
||||
}
|
||||
|
||||
func isPathImage(path string, cache *scanner_cache) bool {
|
||||
if cache.get_photo_type(path) != nil {
|
||||
return true
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
ScannerError("Could not open file %s: %s\n", path, err)
|
||||
return false
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
head := make([]byte, 261)
|
||||
if _, err := file.Read(head); err != nil {
|
||||
if err == io.EOF {
|
||||
return false
|
||||
}
|
||||
|
||||
ScannerError("Could not read file %s: %s\n", path, err)
|
||||
return false
|
||||
}
|
||||
|
||||
imgType, err := filetype.Image(head)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, supported_mime := range SupportedMimetypes {
|
||||
if supported_mime == imgType.MIME.Value {
|
||||
cache.insert_photo_type(path, supported_mime)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Unsupported image %s of type %s\n", path, imgType.MIME.Value)
|
||||
return false
|
||||
}
|
||||
|
||||
func processUnprocessedPhotos(database *sql.DB, user *models.User, notifyKey string) error {
|
||||
|
||||
processKey := utils.GenerateToken()
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
package scanner
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/h2non/filetype"
|
||||
)
|
||||
|
||||
type FileType string
|
||||
|
||||
const (
|
||||
TypeJpeg FileType = "image/jpeg"
|
||||
TypePng FileType = "image/png"
|
||||
TypeTiff FileType = "image/tiff"
|
||||
TypeWebp FileType = "image/webp"
|
||||
TypeCr2 FileType = "image/x-canon-cr2"
|
||||
TypeBmp FileType = "image/bmp"
|
||||
)
|
||||
|
||||
var SupportedMimetypes = [...]FileType{
|
||||
TypeJpeg,
|
||||
TypePng,
|
||||
TypeTiff,
|
||||
TypeWebp,
|
||||
TypeBmp,
|
||||
TypeCr2,
|
||||
}
|
||||
|
||||
var WebMimetypes = [...]FileType{
|
||||
TypeJpeg,
|
||||
TypePng,
|
||||
TypeWebp,
|
||||
TypeBmp,
|
||||
}
|
||||
|
||||
func isPathImage(path string, cache *scanner_cache) bool {
|
||||
if cache.get_photo_type(path) != nil {
|
||||
return true
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
ScannerError("Could not open file %s: %s\n", path, err)
|
||||
return false
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
head := make([]byte, 261)
|
||||
if _, err := file.Read(head); err != nil {
|
||||
if err == io.EOF {
|
||||
return false
|
||||
}
|
||||
|
||||
ScannerError("Could not read file %s: %s\n", path, err)
|
||||
return false
|
||||
}
|
||||
|
||||
imgType, err := filetype.Image(head)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, supported_mime := range SupportedMimetypes {
|
||||
if supported_mime == FileType(imgType.MIME.Value) {
|
||||
cache.insert_photo_type(path, supported_mime)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Unsupported image %s of type %s\n", path, imgType.MIME.Value)
|
||||
return false
|
||||
}
|
|
@ -269,11 +269,11 @@ type ProcessImageData struct {
|
|||
photo *models.Photo
|
||||
_photoImage image.Image
|
||||
_thumbnailImage image.Image
|
||||
_contentType *string
|
||||
_contentType *FileType
|
||||
}
|
||||
|
||||
// ContentType reads the image to determine its content type
|
||||
func (img *ProcessImageData) ContentType() (*string, error) {
|
||||
func (img *ProcessImageData) ContentType() (*FileType, error) {
|
||||
if img._contentType != nil {
|
||||
return img._contentType, nil
|
||||
}
|
||||
|
@ -291,12 +291,13 @@ func (img *ProcessImageData) ContentType() (*string, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
imgType, err := filetype.Image(head)
|
||||
_imgType, err := filetype.Image(head)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
img._contentType = &imgType.MIME.Value
|
||||
imgType := FileType(_imgType.MIME.Value)
|
||||
img._contentType = &imgType
|
||||
return img._contentType, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue