-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
158 lines (133 loc) · 4.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
ffmpeg "github.com/u2takey/ffmpeg-go"
)
var (
logger *logrus.Logger
cacheDir string
artistSquares string
icloudArt string
animatedArt string
)
func init() {
// Initialize logger
logger = logrus.New()
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 - 15:04:05",
DisableSorting: false,
ForceQuote: false,
DisableQuote: true,
ForceColors: true,
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "time",
logrus.FieldKeyLevel: "level",
logrus.FieldKeyMsg: "message",
},
})
// Get the directory of the executable
ex, err := os.Executable()
if err != nil {
logger.Fatalf("Error getting executable path: %v", err)
}
exPath := filepath.Dir(ex)
// Set up directories with absolute paths
cacheDir = filepath.Join(exPath, "cache")
artistSquares = filepath.Join(cacheDir, "artist-squares")
icloudArt = filepath.Join(cacheDir, "icloud-art")
animatedArt = filepath.Join(cacheDir, "animated-art")
logger.Info("AniArt priming up...")
logger.Infof("Published URI: %s", getBaseURI())
logger.Infof("Cache directory: %s", cacheDir)
logger.Infof("Artist Squares directory: %s", artistSquares)
logger.Infof("iCloud Art directory: %s", icloudArt)
logger.Infof("Animated Art directory: %s", animatedArt)
ffmpeg.LogCompiledCommand = false
ensureDirectories()
}
func ensureDirectories() {
dirs := []string{cacheDir, artistSquares, icloudArt, animatedArt}
for _, dir := range dirs {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
logger.Errorf("Error creating directory %s: %v", dir, err)
}
}
}
func main() {
gin.SetMode(gin.ReleaseMode)
gin.ForceConsoleColor()
r := gin.Default()
// Routes
r.GET("/artwork/generate", generateArtwork)
r.GET("/artwork/:key", getArtwork)
r.POST("/artwork/artist-square", generateArtistSquare)
r.GET("/artwork/artist-square/:key", getArtistSquare)
r.POST("/artwork/icloud", generateICloudArt)
r.GET("/artwork/icloud/:key", getICloudArt)
// Experimental, WEBP support.
r.GET("/artwork/generate_alt", generateAltArtwork)
// Start server
if err := r.Run(":3000"); err != nil {
logger.Fatal("Failed to start server: ", err)
}
}
func getArtwork(c *gin.Context) {
key := strings.TrimSuffix(strings.TrimSuffix(c.Param("key"), ".gif"), ".webp")
gifPath := filepath.Join(animatedArt, fmt.Sprintf("%s.gif", key))
webpPath := filepath.Join(animatedArt, fmt.Sprintf("%s.webp", key))
if _, err := os.Stat(gifPath); os.IsNotExist(err) {
if _, err := os.Stat(webpPath); os.IsNotExist(err) {
c.JSON(http.StatusNotFound, gin.H{"error": "Artwork not found"})
return
} else if err != nil {
logger.Errorf("Error accessing WEBP for key %s: %v", key, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error accessing WEBP"})
return
}
c.File(webpPath)
return
} else if err != nil {
logger.Errorf("Error accessing GIF for key %s: %v", key, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error accessing GIF"})
return
}
c.File(gifPath)
}
func getArtistSquare(c *gin.Context) {
key := strings.TrimSuffix(c.Param("key"), ".jpg")
squarePath := filepath.Join(artistSquares, fmt.Sprintf("%s.jpg", key))
if _, err := os.Stat(squarePath); os.IsNotExist(err) {
c.JSON(http.StatusNotFound, gin.H{"error": "Artist Square not found"})
return
} else if err != nil {
logger.Errorf("Error accessing Artist Square for key %s: %v", key, err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error accessing Artist Square"})
return
}
c.File(squarePath)
}
func getICloudArt(c *gin.Context) {
key := c.Param("key")
// Check for each possible format
formats := []string{"jpg", "jpeg", "png", "gif"}
var iCloudPath string
for _, format := range formats {
testPath := filepath.Join(icloudArt, fmt.Sprintf("%s.%s", key, format))
if _, err := os.Stat(testPath); err == nil {
iCloudPath = testPath
break
}
}
if iCloudPath == "" {
c.JSON(http.StatusNotFound, gin.H{"error": "iCloud Art not found"})
return
}
c.File(iCloudPath)
}