codebydante commited on
Commit
e329c12
·
verified ·
1 Parent(s): a9070f6

Upload 115 files

Browse files
Files changed (4) hide show
  1. cmd/fsb/run.go +6 -0
  2. fsb.exe +2 -2
  3. internal/cache/disk.go +47 -0
  4. internal/routes/stream.go +103 -0
cmd/fsb/run.go CHANGED
@@ -10,6 +10,7 @@ import (
10
  "EverythingSuckz/fsb/internal/utils"
11
  "fmt"
12
  "net/http"
 
13
  "time"
14
 
15
  "github.com/spf13/cobra"
@@ -43,6 +44,11 @@ func runApp(cmd *cobra.Command, args []string) {
43
  log.Sugar().Fatalf("Failed to start main bot: %v", err)
44
  }
45
  cache.InitCache(log)
 
 
 
 
 
46
  workers, err := bot.StartWorkers(log)
47
  if err != nil {
48
  log.Sugar().Fatalf("Failed to start workers: %v", err)
 
10
  "EverythingSuckz/fsb/internal/utils"
11
  "fmt"
12
  "net/http"
13
+ "os"
14
  "time"
15
 
16
  "github.com/spf13/cobra"
 
44
  log.Sugar().Fatalf("Failed to start main bot: %v", err)
45
  }
46
  cache.InitCache(log)
47
+
48
+ // Inicializar directorio caché de disco HF
49
+ os.MkdirAll("hf_cache", os.ModePerm)
50
+ go cache.StartDiskCacheEviction("hf_cache", 24*time.Hour, log)
51
+
52
  workers, err := bot.StartWorkers(log)
53
  if err != nil {
54
  log.Sugar().Fatalf("Failed to start workers: %v", err)
fsb.exe CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:f6fe23d0f1a984026e6166829777684b76e4c3149c41b47352c858db0d366725
3
- size 8513024
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:53579946641186adf6eda7215cb2d899028a272f9a2c619b83a93dad8ba3b8df
3
+ size 103047680
internal/cache/disk.go ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ package cache
2
+
3
+ import (
4
+ "os"
5
+ "path/filepath"
6
+ "time"
7
+
8
+ "go.uber.org/zap"
9
+ )
10
+
11
+ // StartDiskCacheEviction periodically deletes files in the cache directory that are older than maxAge.
12
+ func StartDiskCacheEviction(dir string, maxAge time.Duration, log *zap.Logger) {
13
+ log = log.Named("DiskCache")
14
+ ticker := time.NewTicker(1 * time.Hour)
15
+ defer ticker.Stop()
16
+
17
+ for range ticker.C {
18
+ log.Info("Running disk cache eviction cleanup")
19
+ entries, err := os.ReadDir(dir)
20
+ if err != nil {
21
+ log.Error("Failed to read cache directory", zap.Error(err))
22
+ continue
23
+ }
24
+
25
+ now := time.Now()
26
+ var deleted int
27
+ for _, entry := range entries {
28
+ if entry.IsDir() {
29
+ continue
30
+ }
31
+ info, err := entry.Info()
32
+ if err != nil {
33
+ continue
34
+ }
35
+
36
+ if now.Sub(info.ModTime()) > maxAge {
37
+ err = os.Remove(filepath.Join(dir, entry.Name()))
38
+ if err == nil {
39
+ deleted++
40
+ }
41
+ }
42
+ }
43
+ if deleted > 0 {
44
+ log.Sugar().Infof("Evicted %d files from disk cache", deleted)
45
+ }
46
+ }
47
+ }
internal/routes/stream.go CHANGED
@@ -5,20 +5,88 @@ import (
5
  "EverythingSuckz/fsb/internal/stream"
6
  "EverythingSuckz/fsb/internal/types"
7
  "EverythingSuckz/fsb/internal/utils"
 
8
  "fmt"
9
  "io"
10
  "net/http"
 
 
11
  "strconv"
 
 
12
 
13
  "github.com/gotd/td/tg"
14
  range_parser "github.com/quantumsheep/range-parser"
15
  "go.uber.org/zap"
 
16
 
17
  "github.com/gin-gonic/gin"
18
  )
19
 
20
  var log *zap.Logger
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  func (e *allRoutes) LoadHome(r *Route) {
23
  log = e.log.Named("Stream")
24
  defer log.Info("Loaded stream route")
@@ -67,13 +135,33 @@ func getStreamRoute(ctx *gin.Context) {
67
  return
68
  }
69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  // for photo messages
71
  if file.FileSize == 0 {
 
 
 
 
 
72
  res, err := worker.Client.API().UploadGetFile(reqCtx, &tg.UploadGetFileRequest{
73
  Location: file.Location,
74
  Offset: 0,
75
  Limit: 1024 * 1024,
76
  })
 
 
77
  if err != nil {
78
  http.Error(w, err.Error(), http.StatusInternalServerError)
79
  return
@@ -84,6 +172,11 @@ func getStreamRoute(ctx *gin.Context) {
84
  return
85
  }
86
  fileBytes := result.GetBytes()
 
 
 
 
 
87
  ctx.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", file.FileName))
88
  if r.Method != "HEAD" {
89
  ctx.Data(http.StatusOK, file.MimeType, fileBytes)
@@ -131,6 +224,8 @@ func getStreamRoute(ctx *gin.Context) {
131
  ctx.Header("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, file.FileName))
132
 
133
  if r.Method != "HEAD" {
 
 
134
  // Pasamos el reqCtx aquí para que corte la Tubería y las peticiones si el usuario se desconecta
135
  pipe, err := stream.NewStreamPipe(reqCtx, worker.Client, file.Location, start, end, log)
136
  if err != nil {
@@ -138,6 +233,14 @@ func getStreamRoute(ctx *gin.Context) {
138
  return
139
  }
140
  defer pipe.Close()
 
 
 
 
 
 
 
 
141
  if _, err := io.CopyN(w, pipe, contentLength); err != nil {
142
  if !utils.IsClientDisconnectError(err) && err != stream.ErrPipeDrained {
143
  log.Error("Error while copying stream", zap.Error(err))
 
5
  "EverythingSuckz/fsb/internal/stream"
6
  "EverythingSuckz/fsb/internal/types"
7
  "EverythingSuckz/fsb/internal/utils"
8
+ "context"
9
  "fmt"
10
  "io"
11
  "net/http"
12
+ "os"
13
+ "path/filepath"
14
  "strconv"
15
+ "sync"
16
+ "time"
17
 
18
  "github.com/gotd/td/tg"
19
  range_parser "github.com/quantumsheep/range-parser"
20
  "go.uber.org/zap"
21
+ "golang.org/x/sync/semaphore"
22
 
23
  "github.com/gin-gonic/gin"
24
  )
25
 
26
  var log *zap.Logger
27
 
28
+ // Limitador de descargas simultaneas globales para evitar bloqueos por Flood
29
+ var downloadLimiter = semaphore.NewWeighted(60)
30
+
31
+ var dlMut sync.Mutex
32
+ var inProgress = make(map[string]bool)
33
+
34
+ func startBackgroundDownload(loc tg.InputFileLocationClass, fileSize int64, destPath string, l *zap.Logger) {
35
+ dlMut.Lock()
36
+ if inProgress[destPath] {
37
+ dlMut.Unlock()
38
+ return
39
+ }
40
+ inProgress[destPath] = true
41
+ dlMut.Unlock()
42
+
43
+ go func() {
44
+ defer func() {
45
+ dlMut.Lock()
46
+ delete(inProgress, destPath)
47
+ dlMut.Unlock()
48
+ }()
49
+
50
+ if info, err := os.Stat(destPath); err == nil && !info.IsDir() {
51
+ return // ya existe
52
+ }
53
+
54
+ tmpPath := destPath + ".tmp"
55
+ f, err := os.Create(tmpPath)
56
+ if err != nil {
57
+ l.Error("Failed to create temp cache file", zap.Error(err))
58
+ return
59
+ }
60
+ defer func() {
61
+ f.Close()
62
+ os.Remove(tmpPath)
63
+ }()
64
+
65
+ worker := bot.GetNextWorker()
66
+ bgCtx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
67
+ defer cancel()
68
+
69
+ if err := downloadLimiter.Acquire(bgCtx, 1); err != nil {
70
+ return
71
+ }
72
+ defer downloadLimiter.Release(1)
73
+
74
+ pipe, err := stream.NewStreamPipe(bgCtx, worker.Client, loc, 0, fileSize-1, l)
75
+ if err != nil {
76
+ return
77
+ }
78
+ defer pipe.Close()
79
+
80
+ if _, err := io.Copy(f, pipe); err != nil {
81
+ l.Error("Error copying to cache file", zap.Error(err))
82
+ return
83
+ }
84
+ f.Close()
85
+ os.Rename(tmpPath, destPath)
86
+ l.Sugar().Infof("✅ Archivo guardado en caché local en 2do plano: %s", destPath)
87
+ }()
88
+ }
89
+
90
  func (e *allRoutes) LoadHome(r *Route) {
91
  log = e.log.Named("Stream")
92
  defer log.Info("Loaded stream route")
 
135
  return
136
  }
137
 
138
+ cacheFilePath := filepath.Join("hf_cache", fmt.Sprintf("%d_%s", messageID, authHash))
139
+ if info, err := os.Stat(cacheFilePath); err == nil && !info.IsDir() {
140
+ now := time.Now()
141
+ os.Chtimes(cacheFilePath, now, now) // Renovar expiración
142
+
143
+ log.Sugar().Infof("⚡ Servido instantáneo desde disco caché: %s", cacheFilePath)
144
+ ctx.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", file.FileName))
145
+ if r.Method != "HEAD" {
146
+ http.ServeFile(w, r, cacheFilePath)
147
+ }
148
+ return
149
+ }
150
+
151
  // for photo messages
152
  if file.FileSize == 0 {
153
+ // Adquirir semáforo antes de pedir datos a Telegram
154
+ if err := downloadLimiter.Acquire(reqCtx, 1); err != nil {
155
+ http.Error(w, "server too busy", http.StatusTooManyRequests)
156
+ return
157
+ }
158
  res, err := worker.Client.API().UploadGetFile(reqCtx, &tg.UploadGetFileRequest{
159
  Location: file.Location,
160
  Offset: 0,
161
  Limit: 1024 * 1024,
162
  })
163
+ downloadLimiter.Release(1)
164
+
165
  if err != nil {
166
  http.Error(w, err.Error(), http.StatusInternalServerError)
167
  return
 
172
  return
173
  }
174
  fileBytes := result.GetBytes()
175
+
176
+ go func() {
177
+ os.WriteFile(cacheFilePath, fileBytes, 0644)
178
+ }()
179
+
180
  ctx.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", file.FileName))
181
  if r.Method != "HEAD" {
182
  ctx.Data(http.StatusOK, file.MimeType, fileBytes)
 
224
  ctx.Header("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, file.FileName))
225
 
226
  if r.Method != "HEAD" {
227
+ startBackgroundDownload(file.Location, file.FileSize, cacheFilePath, log.Named("BgDownloader"))
228
+
229
  // Pasamos el reqCtx aquí para que corte la Tubería y las peticiones si el usuario se desconecta
230
  pipe, err := stream.NewStreamPipe(reqCtx, worker.Client, file.Location, start, end, log)
231
  if err != nil {
 
233
  return
234
  }
235
  defer pipe.Close()
236
+
237
+ // Adquirir semáforo durante el streaming del archivo
238
+ if err := downloadLimiter.Acquire(reqCtx, 1); err != nil {
239
+ http.Error(w, "server too busy", http.StatusTooManyRequests)
240
+ return
241
+ }
242
+ defer downloadLimiter.Release(1)
243
+
244
  if _, err := io.CopyN(w, pipe, contentLength); err != nil {
245
  if !utils.IsClientDisconnectError(err) && err != stream.ErrPipeDrained {
246
  log.Error("Error while copying stream", zap.Error(err))