diff --git a/internal/library/library.go b/internal/library/library.go index 8a9ff27..89f6dfb 100644 --- a/internal/library/library.go +++ b/internal/library/library.go @@ -2,6 +2,7 @@ package library import ( "app/shelfly/internal/models" + "app/shelfly/query" "encoding/json" "fmt" "io" @@ -12,13 +13,12 @@ import ( "regexp" "strings" "time" + "github.com/gorilla/mux" "gorm.io/gorm" ) - - -func ScanFolder(db *gorm.DB)http.HandlerFunc { +func ScanFolder(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") @@ -37,132 +37,128 @@ func ScanFolder(db *gorm.DB)http.HandlerFunc { // info, _ := os.Stat(existingPath.Path) // fmt.Println(info) err := filepath.Walk(existingPath.Path, func(path string, info os.FileInfo, err error) error { - if err != nil { - fmt.Println(err) - return err - } - - - - fmt.Printf("dir: %v: name: %s\n", info.IsDir(), path) - if(!info.IsDir()){ - //log.Fatalf("Lancement get info") - - fmt.Printf("%s", info.Name()+"\n") - str := info.Name() - str =cleanMovieName(str) - str1 := strings.Replace(str, " ", "+", -1) - url := "https://api.themoviedb.org/3/search/movie?query="+str1+"&language=fr" - fmt.Println(string(url)) - req, _ := http.NewRequest("GET", url, nil) - - req.Header.Add("accept", "application/json") - req.Header.Add("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIzM2VlNTBlOGJlNGQ4YjFkMTYwOTgyMjFhMmEyMjgxOSIsIm5iZiI6MTYwMDc1OTQzOS41NjksInN1YiI6IjVmNjlhNjhmYTZlMmQyMDAzODU5OTVlYiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.wJUlv5oiJhyB7uwb6mcYEnmKh_bh6vC0u0kBuz6ZsGk") - - res, _ := http.DefaultClient.Do(req) - - defer res.Body.Close() - body, _ := io.ReadAll(res.Body) - - var result map[string]interface{} - err = json.Unmarshal(body, &result) - if err != nil { - log.Fatalf("Erreur lors du décodage de la réponse JSON: %v", err) - } - - // Afficher le contenu du résultat - //fmt.Printf("Résultat brut : %+v\n", result) - if results, ok := result["results"].([]interface{}); ok { - for _, item := range results { - if movie, ok := item.(map[string]interface{}); ok { - fmt.Println(movie) - // title := movie["title"] - // releaseDate := movie["release_date"] - // overview := movie["overview"] - - //fmt.Printf("Titre: %v, Date de sortie: %v, Résumé: %v\n", title, releaseDate, overview) - break; - } + if err != nil { + fmt.Println(err) + return err } - } - log.Fatalf("Lancement api") - section := models.LibrarySection{ - Name: "Films", - SectionType: 1, - Language: "fr", - UUID: "section-uuid-123", - CreatedAt: time.Now().String(), - UpdatedAt: time.Now().String(), - } - if err := db.Create(§ion).Error; err != nil { - log.Fatalf("Failed to insert LibrarySection: %v", err) - } - - metadata := models.MetadataItem{ - LibrarySectionID: section.ID, - MetadataType: 1, - GUID: "film-guid-123", - Title: "Inception", - TitleSort: "Inception", - OriginalTitle: "Inception", - Studio: "Warner Bros", - Rating: 8.8, - ContentRating: "PG-13", - Tagline: "Your mind is the scene of the crime.", - Summary: "A skilled thief is offered a chance to erase his criminal past.", - Index: 1, - Duration: 8880, - ReleaseDate: time.Date(2010, 7, 16, 0, 0, 0, 0, time.UTC).String(), - CreatedAt: time.Now().String(), - UpdatedAt: time.Now().String(), - } - if err := db.Create(&metadata).Error; err != nil { - log.Fatalf("Failed to insert MetadataItem: %v", err) - } - - mediaItem := models.MediaItem{ - MetadataItemID: metadata.ID, - Duration: 8880, - Bitrate: 3000, - Width: 1920, - Height: 1080, - AspectRatio: 16.0 / 9.0, - AudioCodec: "AAC", - VideoCodec: "H264", - Container: "MP4", - CreatedAt: time.Now().String(), - UpdatedAt: time.Now().String(), - } - if err := db.Create(&mediaItem).Error; err != nil { - log.Fatalf("Failed to insert MediaItem: %v", err) - } - - mediaPart := models.MediaPart{ - MediaItemID: mediaItem.ID, - File: "/path/to/inception.mp4", - Duration: 8880, - Size: 1500000000, - Indexes: "1", - CreatedAt: time.Now().String(), - UpdatedAt: time.Now().String(), - } - if err := db.Create(&mediaPart).Error; err != nil { - log.Fatalf("Failed to insert MediaPart: %v", err) - } - fmt.Println("Film inserted successfully!") + fmt.Printf("dir: %v: name: %s\n", info.IsDir(), path) + if !info.IsDir() { + //log.Fatalf("Lancement get info") - - } - // api + fmt.Printf("%s", info.Name()+"\n") + str := info.Name() + str = cleanMovieName(str) + str1 := strings.Replace(str, " ", "+", -1) + url := "https://api.themoviedb.org/3/search/movie?query=" + str1 + "&language=fr" + fmt.Println(string(url)) + req, _ := http.NewRequest("GET", url, nil) - - return nil - }) - if err != nil { - fmt.Println(err) - } -} + req.Header.Add("accept", "application/json") + req.Header.Add("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIzM2VlNTBlOGJlNGQ4YjFkMTYwOTgyMjFhMmEyMjgxOSIsIm5iZiI6MTYwMDc1OTQzOS41NjksInN1YiI6IjVmNjlhNjhmYTZlMmQyMDAzODU5OTVlYiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.wJUlv5oiJhyB7uwb6mcYEnmKh_bh6vC0u0kBuz6ZsGk") + + res, _ := http.DefaultClient.Do(req) + + defer res.Body.Close() + body, _ := io.ReadAll(res.Body) + + var result map[string]interface{} + err = json.Unmarshal(body, &result) + if err != nil { + log.Fatalf("Erreur lors du décodage de la réponse JSON: %v", err) + } + + // Afficher le contenu du résultat + //fmt.Printf("Résultat brut : %+v\n", result) + if results, ok := result["results"].([]interface{}); ok { + for _, item := range results { + if movie, ok := item.(map[string]interface{}); ok { + fmt.Println(movie) + // title := movie["title"] + // releaseDate := movie["release_date"] + // overview := movie["overview"] + + //fmt.Printf("Titre: %v, Date de sortie: %v, Résumé: %v\n", title, releaseDate, overview) + break + } + } + } + log.Fatalf("Lancement api") + + section := models.LibrarySection{ + Name: "Films", + SectionType: 1, + Language: "fr", + UUID: "section-uuid-123", + CreatedAt: time.Now().String(), + UpdatedAt: time.Now().String(), + } + if err := db.Create(§ion).Error; err != nil { + log.Fatalf("Failed to insert LibrarySection: %v", err) + } + + metadata := models.MetadataItem{ + LibrarySectionID: section.ID, + MetadataType: 1, + GUID: "film-guid-123", + Title: "Inception", + TitleSort: "Inception", + OriginalTitle: "Inception", + Studio: "Warner Bros", + Rating: 8.8, + ContentRating: "PG-13", + Tagline: "Your mind is the scene of the crime.", + Summary: "A skilled thief is offered a chance to erase his criminal past.", + Index: 1, + Duration: 8880, + ReleaseDate: time.Date(2010, 7, 16, 0, 0, 0, 0, time.UTC).String(), + CreatedAt: time.Now().String(), + UpdatedAt: time.Now().String(), + } + if err := db.Create(&metadata).Error; err != nil { + log.Fatalf("Failed to insert MetadataItem: %v", err) + } + + mediaItem := models.MediaItem{ + MetadataItemID: metadata.ID, + Duration: 8880, + Bitrate: 3000, + Width: 1920, + Height: 1080, + AspectRatio: 16.0 / 9.0, + AudioCodec: "AAC", + VideoCodec: "H264", + Container: "MP4", + CreatedAt: time.Now().String(), + UpdatedAt: time.Now().String(), + } + if err := db.Create(&mediaItem).Error; err != nil { + log.Fatalf("Failed to insert MediaItem: %v", err) + } + + mediaPart := models.MediaPart{ + MediaItemID: mediaItem.ID, + File: "/path/to/inception.mp4", + Duration: 8880, + Size: 1500000000, + Indexes: "1", + CreatedAt: time.Now().String(), + UpdatedAt: time.Now().String(), + } + if err := db.Create(&mediaPart).Error; err != nil { + log.Fatalf("Failed to insert MediaPart: %v", err) + } + fmt.Println("Film inserted successfully!") + + } + // api + + return nil + }) + if err != nil { + fmt.Println(err) + } + } } func cleanMovieName(filename string) string { // Étape 1: Supprimer l'extension du fichier @@ -181,4 +177,31 @@ func cleanMovieName(filename string) string { filename = regexp.MustCompile(`\s+`).ReplaceAllString(filename, " ") return filename -} \ No newline at end of file +} +func CreateDefaultFolder(db *gorm.DB) { + folders := []string{"Film", "Série", "Manga"} + + for _, name := range folders { + path := "upload/" + name + + if _, err := os.Stat(path); os.IsNotExist(err) { + err := os.MkdirAll(path, 0755) // MkdirAll au cas où des dossiers parents manquent + if err != nil { + fmt.Printf("Erreur lors de la création du dossier %s : %v\n", path, err) + } else { + pathDownload := models.PathDownload{ + Path: path, + PathName: name, + } + q := query.Use(db) + if err := q.PathDownload.Create(&pathDownload); err != nil { + fmt.Printf(`{"error": "Failed to create path"}`, http.StatusInternalServerError) + return + } + fmt.Printf("Dossier créé : %s\n", path) + } + } else { + fmt.Printf("Dossier déjà existant : %s\n", path) + } + } +} diff --git a/main.go b/main.go index c63ad99..961d5b2 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "app/shelfly/handlers" "app/shelfly/internal/db" + "app/shelfly/internal/library" "app/shelfly/internal/route" "log" "net/http" @@ -17,6 +18,9 @@ func main() { // 2. Initialiser la DB bd := db.InitDB() + // 2.1 + library.CreateDefaultFolder(bd) + // 3. Routes non protégées : on les monte sur le routeur principal route.RoutesPublic(r, bd) diff --git a/shelfly_db.db b/shelfly_db.db index 665a065..7d27d0e 100644 Binary files a/shelfly_db.db and b/shelfly_db.db differ diff --git a/tmp/main b/tmp/main index 0bb7fbe..e3d8e7f 100644 Binary files a/tmp/main and b/tmp/main differ diff --git a/todo.tx b/todo.tx index 38ced49..11f6b94 100644 --- a/todo.tx +++ b/todo.tx @@ -30,3 +30,16 @@ docker compose -f docker-compose.yml -f docker-compose.prod.yml up --build clean docker system prune -a --volumes + + +Loop golang + +func main() { + // Declare an array of integers + var numbers = [5]int{1, 2, 3, 4, 5} + + // Loop over the array and print each element + for _, number := range numbers { + fmt.Println(number) + } +} \ No newline at end of file