maj
This commit is contained in:
parent
561649d325
commit
4dd0484b80
@ -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")
|
||||||
|
|
||||||
@ -37,132 +37,128 @@ func ScanFolder(db *gorm.DB)http.HandlerFunc {
|
|||||||
// info, _ := os.Stat(existingPath.Path)
|
// info, _ := os.Stat(existingPath.Path)
|
||||||
// fmt.Println(info)
|
// fmt.Println(info)
|
||||||
err := filepath.Walk(existingPath.Path, func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(existingPath.Path, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
log.Fatalf("Lancement api")
|
|
||||||
|
|
||||||
section := models.LibrarySection{
|
fmt.Printf("dir: %v: name: %s\n", info.IsDir(), path)
|
||||||
Name: "Films",
|
if !info.IsDir() {
|
||||||
SectionType: 1,
|
//log.Fatalf("Lancement get info")
|
||||||
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{
|
fmt.Printf("%s", info.Name()+"\n")
|
||||||
LibrarySectionID: section.ID,
|
str := info.Name()
|
||||||
MetadataType: 1,
|
str = cleanMovieName(str)
|
||||||
GUID: "film-guid-123",
|
str1 := strings.Replace(str, " ", "+", -1)
|
||||||
Title: "Inception",
|
url := "https://api.themoviedb.org/3/search/movie?query=" + str1 + "&language=fr"
|
||||||
TitleSort: "Inception",
|
fmt.Println(string(url))
|
||||||
OriginalTitle: "Inception",
|
req, _ := http.NewRequest("GET", url, nil)
|
||||||
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{
|
req.Header.Add("accept", "application/json")
|
||||||
MetadataItemID: metadata.ID,
|
req.Header.Add("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIzM2VlNTBlOGJlNGQ4YjFkMTYwOTgyMjFhMmEyMjgxOSIsIm5iZiI6MTYwMDc1OTQzOS41NjksInN1YiI6IjVmNjlhNjhmYTZlMmQyMDAzODU5OTVlYiIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.wJUlv5oiJhyB7uwb6mcYEnmKh_bh6vC0u0kBuz6ZsGk")
|
||||||
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{
|
res, _ := http.DefaultClient.Do(req)
|
||||||
MediaItemID: mediaItem.ID,
|
|
||||||
File: "/path/to/inception.mp4",
|
defer res.Body.Close()
|
||||||
Duration: 8880,
|
body, _ := io.ReadAll(res.Body)
|
||||||
Size: 1500000000,
|
|
||||||
Indexes: "1",
|
var result map[string]interface{}
|
||||||
CreatedAt: time.Now().String(),
|
err = json.Unmarshal(body, &result)
|
||||||
UpdatedAt: time.Now().String(),
|
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)
|
||||||
}
|
}
|
||||||
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 {
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
4
main.go
4
main.go
@ -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)
|
||||||
|
|
||||||
|
|||||||
BIN
shelfly_db.db
BIN
shelfly_db.db
Binary file not shown.
13
todo.tx
13
todo.tx
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user