This commit is contained in:
julien 2025-06-06 17:05:15 +02:00
parent 561649d325
commit 4dd0484b80
5 changed files with 166 additions and 126 deletions

View File

@ -2,6 +2,7 @@ package library
import ( import (
"app/shelfly/internal/models" "app/shelfly/internal/models"
"app/shelfly/query"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -12,13 +13,12 @@ import (
"regexp" "regexp"
"strings" "strings"
"time" "time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"gorm.io/gorm" "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) { return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@ -42,17 +42,15 @@ func ScanFolder(db *gorm.DB)http.HandlerFunc {
return err return err
} }
fmt.Printf("dir: %v: name: %s\n", info.IsDir(), path) fmt.Printf("dir: %v: name: %s\n", info.IsDir(), path)
if(!info.IsDir()){ if !info.IsDir() {
//log.Fatalf("Lancement get info") //log.Fatalf("Lancement get info")
fmt.Printf("%s", info.Name()+"\n") fmt.Printf("%s", info.Name()+"\n")
str := info.Name() str := info.Name()
str =cleanMovieName(str) str = cleanMovieName(str)
str1 := strings.Replace(str, " ", "+", -1) str1 := strings.Replace(str, " ", "+", -1)
url := "https://api.themoviedb.org/3/search/movie?query="+str1+"&language=fr" url := "https://api.themoviedb.org/3/search/movie?query=" + str1 + "&language=fr"
fmt.Println(string(url)) fmt.Println(string(url))
req, _ := http.NewRequest("GET", url, nil) req, _ := http.NewRequest("GET", url, nil)
@ -81,7 +79,7 @@ func ScanFolder(db *gorm.DB)http.HandlerFunc {
// overview := movie["overview"] // overview := movie["overview"]
//fmt.Printf("Titre: %v, Date de sortie: %v, Résumé: %v\n", title, releaseDate, overview) //fmt.Printf("Titre: %v, Date de sortie: %v, Résumé: %v\n", title, releaseDate, overview)
break; break
} }
} }
} }
@ -152,17 +150,15 @@ func ScanFolder(db *gorm.DB)http.HandlerFunc {
} }
fmt.Println("Film inserted successfully!") fmt.Println("Film inserted successfully!")
} }
// api // api
return nil return nil
}) })
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
} }
} }
func cleanMovieName(filename string) string { func cleanMovieName(filename string) string {
// Étape 1: Supprimer l'extension du fichier // Étape 1: Supprimer l'extension du fichier
@ -182,3 +178,30 @@ func cleanMovieName(filename string) string {
return filename return filename
} }
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)
}
}
}

View File

@ -3,6 +3,7 @@ package main
import ( import (
"app/shelfly/handlers" "app/shelfly/handlers"
"app/shelfly/internal/db" "app/shelfly/internal/db"
"app/shelfly/internal/library"
"app/shelfly/internal/route" "app/shelfly/internal/route"
"log" "log"
"net/http" "net/http"
@ -17,6 +18,9 @@ func main() {
// 2. Initialiser la DB // 2. Initialiser la DB
bd := db.InitDB() bd := db.InitDB()
// 2.1
library.CreateDefaultFolder(bd)
// 3. Routes non protégées : on les monte sur le routeur principal // 3. Routes non protégées : on les monte sur le routeur principal
route.RoutesPublic(r, bd) route.RoutesPublic(r, bd)

Binary file not shown.

BIN
tmp/main

Binary file not shown.

13
todo.tx
View File

@ -30,3 +30,16 @@ docker compose -f docker-compose.yml -f docker-compose.prod.yml up --build
clean clean
docker system prune -a --volumes 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)
}
}