chore: cleanups

This commit is contained in:
2024-10-24 23:53:35 +03:00
parent d6cfbb7b46
commit 100ec54dee
3 changed files with 16 additions and 21 deletions

View File

@@ -162,10 +162,13 @@ func GetScreenshotsDirs() ([]string, error) {
return nil, err
}
for _, entry := range entries {
logger.Debug("Entry: %s", entry.Name())
// logger.Debug("Entry: %s", entry.Name())
if !entry.IsDir() {
continue
}
if slices.Contains(STEAM_INTERNAL_IDS, entry.Name()) {
continue
}
scrDir := fmt.Sprintf("%s/%s/screenshots", remoteDir, entry.Name())
logger.Debug("Checking: %s", scrDir)
if _, err := os.Stat(scrDir); os.IsNotExist(err) {

View File

@@ -12,7 +12,6 @@ import (
"github.com/chenasraf/stimvisor/steam"
)
// screenshots: /Users/chen/Library/Application\ Support/Steam/userdata/USER_ID/760/remote/GAME_ID/screenshots
type ScreenshotCollection struct {
Dir string `json:"dir"`
UserId string `json:"userId"`
@@ -66,9 +65,6 @@ func NewScreenshotsDirFromPath(path string, limit int) ScreenshotCollection {
continue
}
path := fmt.Sprintf("%s/%s", path, f.Name())
// s.Screenshots = append(s.Screenshots, fmt.Sprintf("%s/%s", path, f.Name()))
// convert to base64
// Determine the content type of the image file
bytes, err := os.ReadFile(path)
if err != nil {
logger.FatalErr(err)
@@ -77,34 +73,30 @@ func NewScreenshotsDirFromPath(path string, limit int) ScreenshotCollection {
mimeType := http.DetectContentType(bytes)
// Prepend the appropriate URI scheme header depending
// on the MIME type
var b64 string
switch mimeType {
case "image/jpeg":
b64 += "data:image/jpeg;base64,"
case "image/png":
b64 += "data:image/png;base64,"
default:
supportedMimeTypes := []string{"image/jpeg", "image/png"}
isSupported := strings.Contains(strings.Join(supportedMimeTypes, " "), mimeType)
if !isSupported {
logger.Debug("Unsupported mime type %s for %s", mimeType, f.Name())
continue
}
s.TotalCount++
logger.Debug("Found screenshot %s", path)
if limit > 0 && i > limit {
continue
}
url, _ := strings.CutPrefix(path, steamdir)
url, _ = strings.CutPrefix(url, "/")
entry := ScreenshotEntry{
Dir: filepath.Dir(path),
Path: path,
Name: f.Name(),
Dir: filepath.Dir(path),
Path: path,
Name: f.Name(),
// TODO move URL to consts
URL: fmt.Sprintf("http://localhost:9876/images/%s", url),
MimeType: mimeType,
}
s.Screenshots = append(s.Screenshots, entry)
}
logger.Info("Found %d screenshots for %s", s.TotalCount, s.GameName)
return s
}

View File

@@ -16,6 +16,7 @@ func StartServer() InternalServer {
router.Handle("/images/", http.StripPrefix("/images/", imageHandler{}))
server := InternalServer{}
// TODO use env, and retry other ports when needed
server.Server = &http.Server{Addr: ":9876", Handler: router}
stop := make(chan os.Signal, 1)
@@ -46,18 +47,17 @@ func (h imageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
fullPath := filepath.Join(basedir, path)
logger.Debug("Full path: %s", fullPath)
bytes, err := os.ReadFile(fullPath)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
mimeType := mimetype.Detect(bytes)
logger.Debug("Mime type: %s", mimeType.String())
switch mimeType.String() {
case "image/png", "image/jpeg", "image/gif", "image/webp":
logger.Debug("Serving image: %s", fullPath)
logger.Debug("Serving image: %s (%s)", fullPath, mimeType.String())
http.ServeFile(w, r, fullPath)
default:
logger.Error("Unsupported mime type: %s", mimeType.String())
http.Error(w, "Unsupported image type", http.StatusBadRequest)
}
}