-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scanner: Endpoint for scan and clean
- Loading branch information
1 parent
7e90569
commit 4e883b1
Showing
11 changed files
with
144 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"slices" | ||
|
||
"github.com/Arthi-chaud/Blee/scanner/pkg" | ||
"github.com/Arthi-chaud/Blee/scanner/pkg/actions" | ||
"github.com/Arthi-chaud/Blee/scanner/pkg/api" | ||
"github.com/Arthi-chaud/Blee/scanner/pkg/config" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type ScannerContext struct { | ||
config *config.Config | ||
} | ||
|
||
func getFilesFromAPIOrFail(s *ScannerContext) ([]string, error) { | ||
knownPaths, err := api.GetAllKnownPaths(*s.config) | ||
if err != nil { | ||
return nil, echo.NewHTTPError(http.StatusServiceUnavailable, "Could not get info from API.") | ||
} | ||
return knownPaths, nil | ||
} | ||
|
||
func (s *ScannerContext) Scan(c echo.Context) error { | ||
knownPaths, err := getFilesFromAPIOrFail(s) | ||
if err != nil { | ||
return err | ||
} | ||
go func() { | ||
for _, path := range pkg.GetWatchedFiles(s.config.WatchDir) { | ||
if !slices.Contains(knownPaths, path) && pkg.FileIsVideo(path) { | ||
actions.RegisterFile(path, s.config) | ||
} | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
func (s *ScannerContext) Clean(c echo.Context) error { | ||
knownPaths, err := getFilesFromAPIOrFail(s) | ||
if err != nil { | ||
return err | ||
} | ||
go func() { | ||
watchedFiles := pkg.GetWatchedFiles(s.config.WatchDir) | ||
for _, path := range knownPaths { | ||
if !slices.Contains(watchedFiles, path) && pkg.FileIsVideo(path) { | ||
actions.DeleteFile(path, s.config) | ||
} | ||
} | ||
}() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Get All Files in watched directory | ||
package pkg | ||
|
||
import ( | ||
"os" | ||
"path" | ||
|
||
"github.com/kpango/glg" | ||
) | ||
func GetWatchedFiles(rootDir string) []string { | ||
watchedFiles := []string{} | ||
entries, err := os.ReadDir(rootDir) | ||
|
||
if err != nil { | ||
glg.Fatal(err) | ||
} | ||
|
||
for _, e := range entries { | ||
entryName := path.Join(rootDir, e.Name()) | ||
i, err := os.Stat(entryName) | ||
if err != nil { | ||
glg.Fail(err) | ||
return []string{} | ||
} | ||
if i.IsDir() { | ||
watchedFiles = append(watchedFiles, GetWatchedFiles(entryName)...) | ||
} else { | ||
watchedFiles = append(watchedFiles, entryName) | ||
} | ||
} | ||
return watchedFiles | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.