1
Fork 0

Merge pull request #30 from viktorstrate/api-scan-all

Implement scanAll resolver
This commit is contained in:
Viktor Strate Kløvedal 2020-04-15 11:36:53 +02:00 committed by GitHub
commit 384a54b9a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 7 deletions

View File

@ -9,8 +9,24 @@ import (
) )
func (r *mutationResolver) ScanAll(ctx context.Context) (*models.ScannerResult, error) { func (r *mutationResolver) ScanAll(ctx context.Context) (*models.ScannerResult, error) {
panic("Not implemented") if err := scanner.ScanAll(r.Database); err != nil {
errorMessage := fmt.Sprintf("Error starting scanner: %s", err.Error())
return &models.ScannerResult{
Finished: false,
Success: false,
Message: &errorMessage,
}, nil
} }
startMessage := "Scanner started"
return &models.ScannerResult{
Finished: false,
Success: true,
Message: &startMessage,
}, nil
}
func (r *mutationResolver) ScanUser(ctx context.Context, userID int) (*models.ScannerResult, error) { func (r *mutationResolver) ScanUser(ctx context.Context, userID int) (*models.ScannerResult, error) {
if err := scanner.ScanUser(r.Database, userID); err != nil { if err := scanner.ScanUser(r.Database, userID); err != nil {
errorMessage := fmt.Sprintf("Error scanning user: %s", err.Error()) errorMessage := fmt.Sprintf("Error scanning user: %s", err.Error())

View File

@ -62,6 +62,26 @@ func (cache *scanner_cache) album_contains_photo(path string) *bool {
return nil return nil
} }
func ScanAll(database *sql.DB) error {
rows, err := database.Query("SELECT * FROM user")
if err != nil {
log.Printf("Could not fetch all users from database: %s\n", err.Error())
return err
}
users, err := models.NewUsersFromRows(rows)
if err != nil {
log.Printf("Could not convert users: %s\n", err)
return err
}
for _, user := range users {
go scan(database, user)
}
return nil
}
func ScanUser(database *sql.DB, userId int) error { func ScanUser(database *sql.DB, userId int) error {
row := database.QueryRow("SELECT * FROM user WHERE user_id = ?", userId) row := database.QueryRow("SELECT * FROM user WHERE user_id = ?", userId)
@ -88,7 +108,7 @@ func scan(database *sql.DB, user *models.User) {
Key: notifyKey, Key: notifyKey,
Type: models.NotificationTypeMessage, Type: models.NotificationTypeMessage,
Header: "User scan started", Header: "User scan started",
Content: "Scanning has started...", Content: fmt.Sprintf("Scanning has started for user '%s'", user.Username),
Timeout: &timeout, Timeout: &timeout,
}) })
@ -184,7 +204,7 @@ func scan(database *sql.DB, user *models.User) {
notification.BroadcastNotification(&models.Notification{ notification.BroadcastNotification(&models.Notification{
Key: processKey, Key: processKey,
Type: models.NotificationTypeMessage, Type: models.NotificationTypeMessage,
Header: "Scanning photo", Header: fmt.Sprintf("Scanning photo for user '%s'", user.Username),
Content: fmt.Sprintf("Scanning image at %s", photoPath), Content: fmt.Sprintf("Scanning image at %s", photoPath),
}) })
}) })
@ -220,7 +240,7 @@ func scan(database *sql.DB, user *models.User) {
notification.BroadcastNotification(&models.Notification{ notification.BroadcastNotification(&models.Notification{
Key: notifyKey, Key: notifyKey,
Type: models.NotificationTypeMessage, Type: models.NotificationTypeMessage,
Header: "Scan completed", Header: fmt.Sprintf("Scan completed for user '%s'", user.Username),
Content: completeMessage, Content: completeMessage,
Positive: true, Positive: true,
}) })
@ -232,7 +252,7 @@ func scan(database *sql.DB, user *models.User) {
log.Printf("ERROR: processing photos: %s\n", err) log.Printf("ERROR: processing photos: %s\n", err)
} }
log.Println("Done scanning") log.Printf("Done scanning user '%s'\n", user.Username)
} }
func directoryContainsPhotos(rootPath string, cache *scanner_cache) bool { func directoryContainsPhotos(rootPath string, cache *scanner_cache) bool {
@ -371,7 +391,7 @@ func processUnprocessedPhotos(database *sql.DB, user *models.User, notifyKey str
notification.BroadcastNotification(&models.Notification{ notification.BroadcastNotification(&models.Notification{
Key: processKey, Key: processKey,
Type: models.NotificationTypeProgress, Type: models.NotificationTypeProgress,
Header: fmt.Sprintf("Processing photos (%d of %d)", count, len(photosToProcess)), Header: fmt.Sprintf("Processing photos (%d of %d) for user '%s'", count, len(photosToProcess), user.Username),
Content: fmt.Sprintf("Processing photo at %s", photo.Path), Content: fmt.Sprintf("Processing photo at %s", photo.Path),
Progress: &progress, Progress: &progress,
}) })
@ -395,7 +415,7 @@ func processUnprocessedPhotos(database *sql.DB, user *models.User, notifyKey str
notification.BroadcastNotification(&models.Notification{ notification.BroadcastNotification(&models.Notification{
Key: notifyKey, Key: notifyKey,
Type: models.NotificationTypeMessage, Type: models.NotificationTypeMessage,
Header: "Processing completed", Header: fmt.Sprintf("Processing photos for user '%s' has completed", user.Username),
Content: fmt.Sprintf("%d photos have been processed", len(photosToProcess)), Content: fmt.Sprintf("%d photos have been processed", len(photosToProcess)),
Positive: true, Positive: true,
}) })