Spaces:
Sleeping
Sleeping
Upload 115 files
Browse files- cmd/fsb/run.go +14 -2
- config/config.go +19 -0
- fsb.exe +2 -2
- go.mod +3 -0
- go.sum +6 -0
- internal/cache/disk_cache.go +204 -0
- internal/commands/add_token.go +109 -109
- internal/commands/file_stream.go +294 -294
- internal/routes/stream.go +43 -62
cmd/fsb/run.go
CHANGED
|
@@ -12,6 +12,9 @@ import (
|
|
| 12 |
"net/http"
|
| 13 |
"time"
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
"github.com/spf13/cobra"
|
| 16 |
|
| 17 |
"github.com/gin-gonic/gin"
|
|
@@ -43,6 +46,9 @@ 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)
|
|
@@ -60,8 +66,14 @@ func runApp(cmd *cobra.Command, args []string) {
|
|
| 60 |
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port))
|
| 61 |
mainLogger.Info("File Stream Bot", zap.String("version", versionString))
|
| 62 |
mainLogger.Sugar().Infof("Server is running at %s", config.ValueOf.Host)
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
mainLogger.Sugar().Fatalf("Server failed to start: %v", err)
|
| 66 |
}
|
| 67 |
}
|
|
|
|
| 12 |
"net/http"
|
| 13 |
"time"
|
| 14 |
|
| 15 |
+
"golang.org/x/net/http2"
|
| 16 |
+
"golang.org/x/net/http2/h2c"
|
| 17 |
+
|
| 18 |
"github.com/spf13/cobra"
|
| 19 |
|
| 20 |
"github.com/gin-gonic/gin"
|
|
|
|
| 46 |
log.Sugar().Fatalf("Failed to start main bot: %v", err)
|
| 47 |
}
|
| 48 |
cache.InitCache(log)
|
| 49 |
+
if err := cache.InitDiskCache(log); err != nil {
|
| 50 |
+
log.Sugar().Fatalf("Failed to initialize disk cache: %v", err)
|
| 51 |
+
}
|
| 52 |
workers, err := bot.StartWorkers(log)
|
| 53 |
if err != nil {
|
| 54 |
log.Sugar().Fatalf("Failed to start workers: %v", err)
|
|
|
|
| 66 |
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port))
|
| 67 |
mainLogger.Info("File Stream Bot", zap.String("version", versionString))
|
| 68 |
mainLogger.Sugar().Infof("Server is running at %s", config.ValueOf.Host)
|
| 69 |
+
|
| 70 |
+
h2s := &http2.Server{}
|
| 71 |
+
server := &http.Server{
|
| 72 |
+
Addr: fmt.Sprintf("0.0.0.0:%d", config.ValueOf.Port),
|
| 73 |
+
Handler: h2c.NewHandler(router, h2s),
|
| 74 |
+
}
|
| 75 |
+
err = server.ListenAndServe()
|
| 76 |
+
if err != nil && err != http.ErrServerClosed {
|
| 77 |
mainLogger.Sugar().Fatalf("Server failed to start: %v", err)
|
| 78 |
}
|
| 79 |
}
|
config/config.go
CHANGED
|
@@ -58,6 +58,10 @@ type config struct {
|
|
| 58 |
StreamBufferCount int `envconfig:"STREAM_BUFFER_COUNT" default:"16"`
|
| 59 |
StreamTimeoutSec int `envconfig:"STREAM_TIMEOUT_SEC" default:"30"`
|
| 60 |
StreamMaxRetries int `envconfig:"STREAM_MAX_RETRIES" default:"10"`
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
|
| 63 |
var botTokenRegex = regexp.MustCompile(`MULTI\_TOKEN\d+=(.*)`)
|
|
@@ -95,6 +99,9 @@ func SetFlagsFromConfig(cmd *cobra.Command) {
|
|
| 95 |
cmd.Flags().Int("stream-buffer-count", ValueOf.StreamBufferCount, "Number of blocks to prefetch")
|
| 96 |
cmd.Flags().Int("stream-timeout-sec", ValueOf.StreamTimeoutSec, "Maximum time to wait for a single block (in seconds)")
|
| 97 |
cmd.Flags().Int("stream-max-retries", ValueOf.StreamMaxRetries, "Number of retry attempts for failed fetches")
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
|
| 100 |
func (c *config) loadConfigFromArgs(log *zap.Logger, cmd *cobra.Command) {
|
|
@@ -163,6 +170,18 @@ func (c *config) loadConfigFromArgs(log *zap.Logger, cmd *cobra.Command) {
|
|
| 163 |
if streamMaxRetries != 0 {
|
| 164 |
os.Setenv("STREAM_MAX_RETRIES", strconv.Itoa(streamMaxRetries))
|
| 165 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
}
|
| 167 |
|
| 168 |
func (c *config) setupEnvVars(log *zap.Logger, cmd *cobra.Command) {
|
|
|
|
| 58 |
StreamBufferCount int `envconfig:"STREAM_BUFFER_COUNT" default:"16"`
|
| 59 |
StreamTimeoutSec int `envconfig:"STREAM_TIMEOUT_SEC" default:"30"`
|
| 60 |
StreamMaxRetries int `envconfig:"STREAM_MAX_RETRIES" default:"10"`
|
| 61 |
+
|
| 62 |
+
CacheDir string `envconfig:"CACHE_DIR" default:"cache"`
|
| 63 |
+
MaxConcurrentDownloads int `envconfig:"MAX_CONCURRENT_DOWNLOADS" default:"15"`
|
| 64 |
+
WebPQuality float32 `envconfig:"WEBP_QUALITY" default:"80"`
|
| 65 |
}
|
| 66 |
|
| 67 |
var botTokenRegex = regexp.MustCompile(`MULTI\_TOKEN\d+=(.*)`)
|
|
|
|
| 99 |
cmd.Flags().Int("stream-buffer-count", ValueOf.StreamBufferCount, "Number of blocks to prefetch")
|
| 100 |
cmd.Flags().Int("stream-timeout-sec", ValueOf.StreamTimeoutSec, "Maximum time to wait for a single block (in seconds)")
|
| 101 |
cmd.Flags().Int("stream-max-retries", ValueOf.StreamMaxRetries, "Number of retry attempts for failed fetches")
|
| 102 |
+
cmd.Flags().String("cache-dir", ValueOf.CacheDir, "Directory to store cached files")
|
| 103 |
+
cmd.Flags().Int("max-concurrent-downloads", ValueOf.MaxConcurrentDownloads, "Maximum concurrent Telegram downloads")
|
| 104 |
+
cmd.Flags().Float32("webp-quality", ValueOf.WebPQuality, "Quality for WebP conversion")
|
| 105 |
}
|
| 106 |
|
| 107 |
func (c *config) loadConfigFromArgs(log *zap.Logger, cmd *cobra.Command) {
|
|
|
|
| 170 |
if streamMaxRetries != 0 {
|
| 171 |
os.Setenv("STREAM_MAX_RETRIES", strconv.Itoa(streamMaxRetries))
|
| 172 |
}
|
| 173 |
+
cacheDir, _ := cmd.Flags().GetString("cache-dir")
|
| 174 |
+
if cacheDir != "" {
|
| 175 |
+
os.Setenv("CACHE_DIR", cacheDir)
|
| 176 |
+
}
|
| 177 |
+
maxConcurrentDownloads, _ := cmd.Flags().GetInt("max-concurrent-downloads")
|
| 178 |
+
if maxConcurrentDownloads != 0 {
|
| 179 |
+
os.Setenv("MAX_CONCURRENT_DOWNLOADS", strconv.Itoa(maxConcurrentDownloads))
|
| 180 |
+
}
|
| 181 |
+
webpQuality, _ := cmd.Flags().GetFloat32("webp-quality")
|
| 182 |
+
if webpQuality != 0 {
|
| 183 |
+
os.Setenv("WEBP_QUALITY", strconv.FormatFloat(float64(webpQuality), 'f', -1, 32))
|
| 184 |
+
}
|
| 185 |
}
|
| 186 |
|
| 187 |
func (c *config) setupEnvVars(log *zap.Logger, cmd *cobra.Command) {
|
fsb.exe
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:be5a28d2d6786b2aa52173dddf958599e401abd1abf78cb5d4305bf63fcf073a
|
| 3 |
+
size 103254016
|
go.mod
CHANGED
|
@@ -33,6 +33,7 @@ require (
|
|
| 33 |
github.com/MicahParks/keyfunc v1.9.0 // indirect
|
| 34 |
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
| 35 |
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
|
|
|
| 36 |
github.com/chenzhuoyu/iasm v0.9.1 // indirect
|
| 37 |
github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5 // indirect
|
| 38 |
github.com/coder/websocket v1.8.14 // indirect
|
|
@@ -65,6 +66,8 @@ require (
|
|
| 65 |
github.com/klauspost/compress v1.18.0 // indirect
|
| 66 |
github.com/mattn/go-colorable v0.1.14 // indirect
|
| 67 |
github.com/ncruces/go-strftime v1.0.0 // indirect
|
|
|
|
|
|
|
| 68 |
github.com/ogen-go/ogen v1.16.0 // indirect
|
| 69 |
github.com/pkg/errors v0.9.1 // indirect
|
| 70 |
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
|
|
|
|
| 33 |
github.com/MicahParks/keyfunc v1.9.0 // indirect
|
| 34 |
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
| 35 |
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
| 36 |
+
github.com/chai2010/webp v1.4.0 // indirect
|
| 37 |
github.com/chenzhuoyu/iasm v0.9.1 // indirect
|
| 38 |
github.com/cncf/xds/go v0.0.0-20251210132809-ee656c7534f5 // indirect
|
| 39 |
github.com/coder/websocket v1.8.14 // indirect
|
|
|
|
| 66 |
github.com/klauspost/compress v1.18.0 // indirect
|
| 67 |
github.com/mattn/go-colorable v0.1.14 // indirect
|
| 68 |
github.com/ncruces/go-strftime v1.0.0 // indirect
|
| 69 |
+
github.com/nickalie/go-binwrapper v0.0.0-20260217210625-2cc2a03abe15 // indirect
|
| 70 |
+
github.com/nickalie/go-webpbin v0.0.0-20260302110202-4584fd694f25 // indirect
|
| 71 |
github.com/ogen-go/ogen v1.16.0 // indirect
|
| 72 |
github.com/pkg/errors v0.9.1 // indirect
|
| 73 |
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
|
go.sum
CHANGED
|
@@ -47,6 +47,8 @@ github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY
|
|
| 47 |
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
| 48 |
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
| 49 |
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
|
|
|
|
|
|
| 50 |
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
| 51 |
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
| 52 |
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
|
|
@@ -186,6 +188,10 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
|
|
| 186 |
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
| 187 |
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
| 188 |
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
github.com/ogen-go/ogen v1.16.0 h1:fKHEYokW/QrMzVNXId74/6RObRIUs9T2oroGKtR25Iw=
|
| 190 |
github.com/ogen-go/ogen v1.16.0/go.mod h1:s3nWiMzybSf8fhxckyO+wtto92+QHpEL8FmkPnhL3jI=
|
| 191 |
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
|
|
|
|
| 47 |
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
| 48 |
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
| 49 |
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
| 50 |
+
github.com/chai2010/webp v1.4.0 h1:6DA2pkkRUPnbOHvvsmGI3He1hBKf/bkRlniAiSGuEko=
|
| 51 |
+
github.com/chai2010/webp v1.4.0/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
|
| 52 |
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
|
| 53 |
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
|
| 54 |
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
|
|
|
|
| 188 |
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
| 189 |
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
| 190 |
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
| 191 |
+
github.com/nickalie/go-binwrapper v0.0.0-20260217210625-2cc2a03abe15 h1:PRBiX3Nf8l++Sp0YrgtvbL1voYxcIb/Mzt9I5Qa5r6I=
|
| 192 |
+
github.com/nickalie/go-binwrapper v0.0.0-20260217210625-2cc2a03abe15/go.mod h1:93jel0A9klQ6KkU/3u4XF+fb1Z0pPb/bSTIosDHe5fU=
|
| 193 |
+
github.com/nickalie/go-webpbin v0.0.0-20260302110202-4584fd694f25 h1:RnVu6/asSFRqqIec1ManYx24VMrfCDpCHpbDNX7g4fw=
|
| 194 |
+
github.com/nickalie/go-webpbin v0.0.0-20260302110202-4584fd694f25/go.mod h1:wYwiyR1OU20qsxvjkACm15MTb5FYuYuso6oWMxaFdkk=
|
| 195 |
github.com/ogen-go/ogen v1.16.0 h1:fKHEYokW/QrMzVNXId74/6RObRIUs9T2oroGKtR25Iw=
|
| 196 |
github.com/ogen-go/ogen v1.16.0/go.mod h1:s3nWiMzybSf8fhxckyO+wtto92+QHpEL8FmkPnhL3jI=
|
| 197 |
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
|
internal/cache/disk_cache.go
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
package cache
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"EverythingSuckz/fsb/config"
|
| 5 |
+
"context"
|
| 6 |
+
"fmt"
|
| 7 |
+
"io"
|
| 8 |
+
"os"
|
| 9 |
+
"path/filepath"
|
| 10 |
+
"strconv"
|
| 11 |
+
"time"
|
| 12 |
+
|
| 13 |
+
"github.com/nickalie/go-webpbin"
|
| 14 |
+
"go.uber.org/zap"
|
| 15 |
+
"golang.org/x/sync/singleflight"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
var (
|
| 19 |
+
Disk *DiskCache
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
type DiskCache struct {
|
| 23 |
+
dir string
|
| 24 |
+
sf singleflight.Group
|
| 25 |
+
log *zap.Logger
|
| 26 |
+
downloadSem chan struct{}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
func InitDiskCache(log *zap.Logger) error {
|
| 30 |
+
dir := config.ValueOf.CacheDir
|
| 31 |
+
if err := os.MkdirAll(dir, 0755); err != nil {
|
| 32 |
+
return fmt.Errorf("failed to create cache directory: %w", err)
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
Disk = &DiskCache{
|
| 36 |
+
dir: dir,
|
| 37 |
+
log: log.Named("DiskCache"),
|
| 38 |
+
downloadSem: make(chan struct{}, config.ValueOf.MaxConcurrentDownloads),
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// Start cache sweeper (every 1 hour, delete files older than 24h)
|
| 42 |
+
go Disk.startSweeper()
|
| 43 |
+
|
| 44 |
+
Disk.log.Info("Disk cache initialized", zap.String("dir", dir), zap.Int("max_downloads", config.ValueOf.MaxConcurrentDownloads))
|
| 45 |
+
return nil
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
// GetOrDownload returns the path to the cached original file.
|
| 49 |
+
// If not exists, executes the fetcher under a singleflight lock and a semaphore.
|
| 50 |
+
func (c *DiskCache) GetOrDownload(ctx context.Context, messageID int, fetcher func() (io.ReadCloser, error)) (string, error) {
|
| 51 |
+
filename := strconv.Itoa(messageID)
|
| 52 |
+
path := filepath.Join(c.dir, filename)
|
| 53 |
+
|
| 54 |
+
if c.fileExists(path) {
|
| 55 |
+
// return directly
|
| 56 |
+
return path, nil
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
// fetch with singleflight to avoid duplicated downloads
|
| 60 |
+
result, err, _ := c.sf.Do(filename, func() (interface{}, error) {
|
| 61 |
+
if c.fileExists(path) {
|
| 62 |
+
return path, nil
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
c.log.Sugar().Infof("Starting download to disk cache: %s", filename)
|
| 66 |
+
|
| 67 |
+
// Acquire semaphore
|
| 68 |
+
select {
|
| 69 |
+
case c.downloadSem <- struct{}{}:
|
| 70 |
+
case <-ctx.Done():
|
| 71 |
+
return nil, ctx.Err()
|
| 72 |
+
}
|
| 73 |
+
defer func() { <-c.downloadSem }()
|
| 74 |
+
|
| 75 |
+
reader, err := fetcher()
|
| 76 |
+
if err != nil {
|
| 77 |
+
return nil, err
|
| 78 |
+
}
|
| 79 |
+
defer reader.Close()
|
| 80 |
+
|
| 81 |
+
tempPath := path + ".tmp"
|
| 82 |
+
fw, err := os.Create(tempPath)
|
| 83 |
+
if err != nil {
|
| 84 |
+
return nil, err
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
if _, err := io.Copy(fw, reader); err != nil {
|
| 88 |
+
fw.Close()
|
| 89 |
+
os.Remove(tempPath)
|
| 90 |
+
return nil, err
|
| 91 |
+
}
|
| 92 |
+
fw.Close()
|
| 93 |
+
|
| 94 |
+
if err := os.Rename(tempPath, path); err != nil {
|
| 95 |
+
os.Remove(tempPath)
|
| 96 |
+
return nil, err
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
c.log.Sugar().Infof("Cached file saved: %s", filename)
|
| 100 |
+
return path, nil
|
| 101 |
+
})
|
| 102 |
+
|
| 103 |
+
if err != nil {
|
| 104 |
+
return "", err
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
return result.(string), nil
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// GetOrConvertWebP returns the path to the cached WebP file.
|
| 111 |
+
// It uses GetOrDownload first to ensure the original is present, then converts.
|
| 112 |
+
func (c *DiskCache) GetOrConvertWebP(ctx context.Context, messageID int, fetcher func() (io.ReadCloser, error)) (string, error) {
|
| 113 |
+
webpFilename := strconv.Itoa(messageID) + "_webp"
|
| 114 |
+
webpPath := filepath.Join(c.dir, webpFilename)
|
| 115 |
+
|
| 116 |
+
if c.fileExists(webpPath) {
|
| 117 |
+
return webpPath, nil
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
result, err, _ := c.sf.Do(webpFilename, func() (interface{}, error) {
|
| 121 |
+
if c.fileExists(webpPath) {
|
| 122 |
+
return webpPath, nil
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// First, get the original file
|
| 126 |
+
origPath, err := c.GetOrDownload(ctx, messageID, fetcher)
|
| 127 |
+
if err != nil {
|
| 128 |
+
return nil, err
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
c.log.Sugar().Infof("Starting WebP conversion for: %s", webpFilename)
|
| 132 |
+
|
| 133 |
+
tempPath := webpPath + ".tmp"
|
| 134 |
+
|
| 135 |
+
err = webpbin.NewCWebP().
|
| 136 |
+
Quality(uint(config.ValueOf.WebPQuality)).
|
| 137 |
+
InputFile(origPath).
|
| 138 |
+
OutputFile(tempPath).
|
| 139 |
+
Run()
|
| 140 |
+
|
| 141 |
+
if err != nil {
|
| 142 |
+
return nil, fmt.Errorf("failed to encode webp: %w", err)
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
if err := os.Rename(tempPath, webpPath); err != nil {
|
| 146 |
+
os.Remove(tempPath)
|
| 147 |
+
return nil, err
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
c.log.Sugar().Infof("WebP cached file saved: %s", webpFilename)
|
| 151 |
+
return webpPath, nil
|
| 152 |
+
})
|
| 153 |
+
|
| 154 |
+
if err != nil {
|
| 155 |
+
return "", err
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
return result.(string), nil
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
func (c *DiskCache) fileExists(path string) bool {
|
| 162 |
+
info, err := os.Stat(path)
|
| 163 |
+
if err == nil && !info.IsDir() {
|
| 164 |
+
return true
|
| 165 |
+
}
|
| 166 |
+
return false
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
func (c *DiskCache) startSweeper() {
|
| 170 |
+
ticker := time.NewTicker(time.Hour)
|
| 171 |
+
defer ticker.Stop()
|
| 172 |
+
|
| 173 |
+
for range ticker.C {
|
| 174 |
+
c.log.Info("Running cache cleanup sweeper")
|
| 175 |
+
threshold := time.Now().Add(-24 * time.Hour)
|
| 176 |
+
|
| 177 |
+
entries, err := os.ReadDir(c.dir)
|
| 178 |
+
if err != nil {
|
| 179 |
+
c.log.Error("Failed to read cache dir for sweep", zap.Error(err))
|
| 180 |
+
continue
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
count := 0
|
| 184 |
+
for _, e := range entries {
|
| 185 |
+
if e.IsDir() {
|
| 186 |
+
continue
|
| 187 |
+
}
|
| 188 |
+
path := filepath.Join(c.dir, e.Name())
|
| 189 |
+
info, err := e.Info()
|
| 190 |
+
if err != nil {
|
| 191 |
+
continue
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
if info.ModTime().Before(threshold) {
|
| 195 |
+
os.Remove(path)
|
| 196 |
+
count++
|
| 197 |
+
}
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
if count > 0 {
|
| 201 |
+
c.log.Sugar().Infof("Cleared %d old files from disk cache", count)
|
| 202 |
+
}
|
| 203 |
+
}
|
| 204 |
+
}
|
internal/commands/add_token.go
CHANGED
|
@@ -1,109 +1,109 @@
|
|
| 1 |
-
package commands
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
"fmt"
|
| 5 |
-
"regexp"
|
| 6 |
-
"strings"
|
| 7 |
-
|
| 8 |
-
"github.com/celestix/gotgproto/dispatcher"
|
| 9 |
-
"github.com/celestix/gotgproto/dispatcher/handlers"
|
| 10 |
-
"github.com/celestix/gotgproto/ext"
|
| 11 |
-
"go.uber.org/zap"
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
var (
|
| 15 |
-
isAddTokenMode = false
|
| 16 |
-
botTokenRegex = regexp.MustCompile(`^\d{9,10}:[A-Za-z0-9_-]{35}$`) // Standard Telegram bot token format
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
func (c *command) AddTokenOn(d dispatcher.Dispatcher) {
|
| 20 |
-
d.AddHandler(handlers.NewCommand("add_token_on", func(ctx *ext.Context, u *ext.Update) error {
|
| 21 |
-
isAddTokenMode = true
|
| 22 |
-
c.log.Sugar().Info("Add token mode activated")
|
| 23 |
-
_, err := ctx.Reply(u, ext.ReplyTextString("✅ Modo para añadir tokens activado. Envía tokens de bot (uno por mensaje) y los guardaré en la base de datos de Firebase."), nil)
|
| 24 |
-
return err
|
| 25 |
-
}))
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
func (c *command) AddTokenOff(d dispatcher.Dispatcher) {
|
| 29 |
-
d.AddHandler(handlers.NewCommand("add_token_off", func(ctx *ext.Context, u *ext.Update) error {
|
| 30 |
-
isAddTokenMode = false
|
| 31 |
-
c.log.Sugar().Info("Add token mode deactivated")
|
| 32 |
-
_, err := ctx.Reply(u, ext.ReplyTextString("❌ Modo para añadir tokens desactivado."), nil)
|
| 33 |
-
return err
|
| 34 |
-
}))
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
func (c *command) ListTokens(d dispatcher.Dispatcher) {
|
| 38 |
-
d.AddHandler(handlers.NewCommand("list", func(ctx *ext.Context, u *ext.Update) error {
|
| 39 |
-
if GetWorkerListFunc == nil {
|
| 40 |
-
_, err := ctx.Reply(u, ext.ReplyTextString("❌ El sistema de lista de tokens no está conectado."), nil)
|
| 41 |
-
return err
|
| 42 |
-
}
|
| 43 |
-
listText := GetWorkerListFunc(true)
|
| 44 |
-
_, err := ctx.Reply(u, ext.ReplyTextString(listText), nil)
|
| 45 |
-
return err
|
| 46 |
-
}))
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
func (c *command) HandleBotTokenMessage(d dispatcher.Dispatcher) {
|
| 50 |
-
d.AddHandlerToGroup(
|
| 51 |
-
handlers.NewMessage(nil, func(ctx *ext.Context, u *ext.Update) error {
|
| 52 |
-
if !isAddTokenMode {
|
| 53 |
-
return nil
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
if u.EffectiveMessage == nil {
|
| 57 |
-
return nil
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
messageText := u.EffectiveMessage.Message.Message
|
| 61 |
-
if messageText == "" {
|
| 62 |
-
return nil
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
// Validate if message looks like a token
|
| 66 |
-
token := strings.TrimSpace(messageText)
|
| 67 |
-
if !botTokenRegex.MatchString(token) {
|
| 68 |
-
// Not a token, ignore
|
| 69 |
-
return nil
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
c.log.Sugar().Infof("Received a new bot token: %s", token)
|
| 73 |
-
|
| 74 |
-
if SaveTokenFunc == nil || StartWorkerFunc == nil {
|
| 75 |
-
_, err := ctx.Reply(u, ext.ReplyTextString("❌ El sistema de tokens dinámicos no está listo."), nil)
|
| 76 |
-
return err
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
// 1. Save it to Firebase
|
| 80 |
-
err := SaveTokenFunc(token)
|
| 81 |
-
if err != nil {
|
| 82 |
-
if strings.Contains(err.Error(), "ya existe") {
|
| 83 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("⚠️ Este token ya está registrado en Firebase."), nil)
|
| 84 |
-
} else {
|
| 85 |
-
c.log.Error("Failed to save token to firebase", zap.Error(err))
|
| 86 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("❌ Error al guardar el token en Firebase: "+err.Error()), nil)
|
| 87 |
-
}
|
| 88 |
-
return nil
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
// Respond immediately to stop the webhook from retrying and causing duplicates
|
| 92 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("⏳ El token ha sido guardado exitosamente en Firebase. Conectando al Servidor de Telegram..."), nil)
|
| 93 |
-
|
| 94 |
-
// 2. Add it to the dynamic worker pool in the background
|
| 95 |
-
go func(t string) {
|
| 96 |
-
username, startErr := StartWorkerFunc(t)
|
| 97 |
-
if startErr != nil {
|
| 98 |
-
c.log.Error("Failed to start worker with new token", zap.Error(startErr))
|
| 99 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("⚠️ El bot se guardó, pero falló al unirse (¿Token inválido?): "+startErr.Error()), nil)
|
| 100 |
-
} else {
|
| 101 |
-
successText := fmt.Sprintf("@%s\n`%s`\n\nse agrego correctamente al fire base", username, t)
|
| 102 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString(successText), nil)
|
| 103 |
-
}
|
| 104 |
-
}(token)
|
| 105 |
-
|
| 106 |
-
return nil
|
| 107 |
-
|
| 108 |
-
}), -1) // Group -1 para que atrape los mensajes ANTES que el handler de streams del Group 0
|
| 109 |
-
}
|
|
|
|
| 1 |
+
package commands
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"fmt"
|
| 5 |
+
"regexp"
|
| 6 |
+
"strings"
|
| 7 |
+
|
| 8 |
+
"github.com/celestix/gotgproto/dispatcher"
|
| 9 |
+
"github.com/celestix/gotgproto/dispatcher/handlers"
|
| 10 |
+
"github.com/celestix/gotgproto/ext"
|
| 11 |
+
"go.uber.org/zap"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
var (
|
| 15 |
+
isAddTokenMode = false
|
| 16 |
+
botTokenRegex = regexp.MustCompile(`^\d{9,10}:[A-Za-z0-9_-]{35}$`) // Standard Telegram bot token format
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
func (c *command) AddTokenOn(d dispatcher.Dispatcher) {
|
| 20 |
+
d.AddHandler(handlers.NewCommand("add_token_on", func(ctx *ext.Context, u *ext.Update) error {
|
| 21 |
+
isAddTokenMode = true
|
| 22 |
+
c.log.Sugar().Info("Add token mode activated")
|
| 23 |
+
_, err := ctx.Reply(u, ext.ReplyTextString("✅ Modo para añadir tokens activado. Envía tokens de bot (uno por mensaje) y los guardaré en la base de datos de Firebase."), nil)
|
| 24 |
+
return err
|
| 25 |
+
}))
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
func (c *command) AddTokenOff(d dispatcher.Dispatcher) {
|
| 29 |
+
d.AddHandler(handlers.NewCommand("add_token_off", func(ctx *ext.Context, u *ext.Update) error {
|
| 30 |
+
isAddTokenMode = false
|
| 31 |
+
c.log.Sugar().Info("Add token mode deactivated")
|
| 32 |
+
_, err := ctx.Reply(u, ext.ReplyTextString("❌ Modo para añadir tokens desactivado."), nil)
|
| 33 |
+
return err
|
| 34 |
+
}))
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
func (c *command) ListTokens(d dispatcher.Dispatcher) {
|
| 38 |
+
d.AddHandler(handlers.NewCommand("list", func(ctx *ext.Context, u *ext.Update) error {
|
| 39 |
+
if GetWorkerListFunc == nil {
|
| 40 |
+
_, err := ctx.Reply(u, ext.ReplyTextString("❌ El sistema de lista de tokens no está conectado."), nil)
|
| 41 |
+
return err
|
| 42 |
+
}
|
| 43 |
+
listText := GetWorkerListFunc(true)
|
| 44 |
+
_, err := ctx.Reply(u, ext.ReplyTextString(listText), nil)
|
| 45 |
+
return err
|
| 46 |
+
}))
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
func (c *command) HandleBotTokenMessage(d dispatcher.Dispatcher) {
|
| 50 |
+
d.AddHandlerToGroup(
|
| 51 |
+
handlers.NewMessage(nil, func(ctx *ext.Context, u *ext.Update) error {
|
| 52 |
+
if !isAddTokenMode {
|
| 53 |
+
return nil
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
if u.EffectiveMessage == nil {
|
| 57 |
+
return nil
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
messageText := u.EffectiveMessage.Message.Message
|
| 61 |
+
if messageText == "" {
|
| 62 |
+
return nil
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
// Validate if message looks like a token
|
| 66 |
+
token := strings.TrimSpace(messageText)
|
| 67 |
+
if !botTokenRegex.MatchString(token) {
|
| 68 |
+
// Not a token, ignore
|
| 69 |
+
return nil
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
c.log.Sugar().Infof("Received a new bot token: %s", token)
|
| 73 |
+
|
| 74 |
+
if SaveTokenFunc == nil || StartWorkerFunc == nil {
|
| 75 |
+
_, err := ctx.Reply(u, ext.ReplyTextString("❌ El sistema de tokens dinámicos no está listo."), nil)
|
| 76 |
+
return err
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
// 1. Save it to Firebase
|
| 80 |
+
err := SaveTokenFunc(token)
|
| 81 |
+
if err != nil {
|
| 82 |
+
if strings.Contains(err.Error(), "ya existe") {
|
| 83 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("⚠️ Este token ya está registrado en Firebase."), nil)
|
| 84 |
+
} else {
|
| 85 |
+
c.log.Error("Failed to save token to firebase", zap.Error(err))
|
| 86 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("❌ Error al guardar el token en Firebase: "+err.Error()), nil)
|
| 87 |
+
}
|
| 88 |
+
return nil
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
// Respond immediately to stop the webhook from retrying and causing duplicates
|
| 92 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("⏳ El token ha sido guardado exitosamente en Firebase. Conectando al Servidor de Telegram..."), nil)
|
| 93 |
+
|
| 94 |
+
// 2. Add it to the dynamic worker pool in the background
|
| 95 |
+
go func(t string) {
|
| 96 |
+
username, startErr := StartWorkerFunc(t)
|
| 97 |
+
if startErr != nil {
|
| 98 |
+
c.log.Error("Failed to start worker with new token", zap.Error(startErr))
|
| 99 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("⚠️ El bot se guardó, pero falló al unirse (¿Token inválido?): "+startErr.Error()), nil)
|
| 100 |
+
} else {
|
| 101 |
+
successText := fmt.Sprintf("@%s\n`%s`\n\nse agrego correctamente al fire base", username, t)
|
| 102 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString(successText), nil)
|
| 103 |
+
}
|
| 104 |
+
}(token)
|
| 105 |
+
|
| 106 |
+
return nil
|
| 107 |
+
|
| 108 |
+
}), -1) // Group -1 para que atrape los mensajes ANTES que el handler de streams del Group 0
|
| 109 |
+
}
|
internal/commands/file_stream.go
CHANGED
|
@@ -1,294 +1,294 @@
|
|
| 1 |
-
package commands
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
"bufio"
|
| 5 |
-
"bytes"
|
| 6 |
-
"fmt"
|
| 7 |
-
"sort"
|
| 8 |
-
"strconv"
|
| 9 |
-
"strings"
|
| 10 |
-
"sync"
|
| 11 |
-
"time"
|
| 12 |
-
|
| 13 |
-
"EverythingSuckz/fsb/config"
|
| 14 |
-
"EverythingSuckz/fsb/internal/utils"
|
| 15 |
-
|
| 16 |
-
"github.com/celestix/gotgproto/dispatcher"
|
| 17 |
-
"github.com/celestix/gotgproto/ext"
|
| 18 |
-
"github.com/gotd/td/telegram/downloader"
|
| 19 |
-
"github.com/gotd/td/tg"
|
| 20 |
-
"go.uber.org/zap"
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
type FileStreamSession struct {
|
| 24 |
-
Active bool
|
| 25 |
-
mu sync.Mutex
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
var (
|
| 29 |
-
fileStreamSessions = make(map[int64]*FileStreamSession)
|
| 30 |
-
fileStreamMu sync.Mutex
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
func ToggleFileStream(ctx *ext.Context, u *ext.Update) error {
|
| 34 |
-
chatId := u.EffectiveChat().GetID()
|
| 35 |
-
messageText := u.EffectiveMessage.Message.Message
|
| 36 |
-
args := strings.Split(messageText, " ")
|
| 37 |
-
cmd := strings.ToLower(args[0])
|
| 38 |
-
|
| 39 |
-
var state string
|
| 40 |
-
if strings.Contains(cmd, "on") {
|
| 41 |
-
state = "on"
|
| 42 |
-
} else if strings.Contains(cmd, "off") {
|
| 43 |
-
state = "off"
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
if state == "" {
|
| 47 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("Uso: /file_id_stream_on o /file_id_stream_off"), nil)
|
| 48 |
-
return dispatcher.EndGroups
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
fileStreamMu.Lock()
|
| 52 |
-
session, ok := fileStreamSessions[chatId]
|
| 53 |
-
if !ok {
|
| 54 |
-
session = &FileStreamSession{Active: false}
|
| 55 |
-
fileStreamSessions[chatId] = session
|
| 56 |
-
}
|
| 57 |
-
fileStreamMu.Unlock()
|
| 58 |
-
|
| 59 |
-
session.mu.Lock()
|
| 60 |
-
defer session.mu.Unlock()
|
| 61 |
-
|
| 62 |
-
if state == "on" {
|
| 63 |
-
if session.Active {
|
| 64 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("File Stream ya está activado."), nil)
|
| 65 |
-
return dispatcher.EndGroups
|
| 66 |
-
}
|
| 67 |
-
session.Active = true
|
| 68 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("✅ File Stream activado. Envía tu archivo .txt con File IDs."), nil)
|
| 69 |
-
return dispatcher.EndGroups
|
| 70 |
-
} else if state == "off" {
|
| 71 |
-
if !session.Active {
|
| 72 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("File Stream ya está desactivado."), nil)
|
| 73 |
-
return dispatcher.EndGroups
|
| 74 |
-
}
|
| 75 |
-
session.Active = false
|
| 76 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("❌ File Stream desactivado."), nil)
|
| 77 |
-
return dispatcher.EndGroups
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
return dispatcher.EndGroups
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
func GetFileStreamSession(chatId int64) *FileStreamSession {
|
| 84 |
-
fileStreamMu.Lock()
|
| 85 |
-
defer fileStreamMu.Unlock()
|
| 86 |
-
return fileStreamSessions[chatId]
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
type ParsedFileItem struct {
|
| 90 |
-
Type string // "ORIGINAL" or "WEBP"
|
| 91 |
-
Name string
|
| 92 |
-
MessageID int
|
| 93 |
-
HFLink string
|
| 94 |
-
LocalLink string
|
| 95 |
-
}
|
| 96 |
-
|
| 97 |
-
func HandleFileStreamFile(chatId int64, ctx *ext.Context, u *ext.Update, fileMedia interface{}) error {
|
| 98 |
-
fileDoc, err := utils.FileFromMedia(fileMedia.(tg.MessageMediaClass))
|
| 99 |
-
if err != nil {
|
| 100 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Error obteniendo media de telegram: %v", err)), nil)
|
| 101 |
-
return err
|
| 102 |
-
}
|
| 103 |
-
|
| 104 |
-
// Download the .txt file
|
| 105 |
-
d := downloader.NewDownloader()
|
| 106 |
-
var docBuf bytes.Buffer
|
| 107 |
-
_, err = d.Download(ctx.Raw, fileDoc.Location).Stream(ctx.Context, &docBuf)
|
| 108 |
-
if err != nil {
|
| 109 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Error descargando el archivo: %v", err)), nil)
|
| 110 |
-
return err
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
-
fileBytes := docBuf.Bytes()
|
| 114 |
-
|
| 115 |
-
// Parse the .txt content
|
| 116 |
-
scanner := bufio.NewScanner(bytes.NewReader(fileBytes))
|
| 117 |
-
var title string = "FileStream_Collection"
|
| 118 |
-
var currentItem *ParsedFileItem
|
| 119 |
-
var items []ParsedFileItem
|
| 120 |
-
|
| 121 |
-
for scanner.Scan() {
|
| 122 |
-
line := strings.TrimSpace(scanner.Text())
|
| 123 |
-
if line == "" || strings.HasPrefix(line, "--") {
|
| 124 |
-
continue
|
| 125 |
-
}
|
| 126 |
-
|
| 127 |
-
if strings.HasPrefix(line, "=== Datos del ZIP: ") {
|
| 128 |
-
titleRaw := strings.TrimPrefix(line, "=== Datos del ZIP: ")
|
| 129 |
-
titleRaw = strings.TrimSuffix(titleRaw, " ===")
|
| 130 |
-
title = strings.TrimSuffix(titleRaw, ".zip")
|
| 131 |
-
title = strings.TrimSpace(title)
|
| 132 |
-
continue
|
| 133 |
-
}
|
| 134 |
-
|
| 135 |
-
if strings.HasPrefix(line, "[ORIGINAL]") {
|
| 136 |
-
if currentItem != nil && currentItem.MessageID != 0 {
|
| 137 |
-
items = append(items, *currentItem)
|
| 138 |
-
}
|
| 139 |
-
currentItem = &ParsedFileItem{
|
| 140 |
-
Type: "ORIGINAL",
|
| 141 |
-
Name: strings.TrimSpace(strings.TrimPrefix(line, "[ORIGINAL]")),
|
| 142 |
-
}
|
| 143 |
-
continue
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
if strings.HasPrefix(line, "[WEBP]") {
|
| 147 |
-
if currentItem != nil && currentItem.MessageID != 0 {
|
| 148 |
-
items = append(items, *currentItem)
|
| 149 |
-
}
|
| 150 |
-
currentItem = &ParsedFileItem{
|
| 151 |
-
Type: "WEBP",
|
| 152 |
-
Name: strings.TrimSpace(strings.TrimPrefix(line, "[WEBP]")),
|
| 153 |
-
}
|
| 154 |
-
continue
|
| 155 |
-
}
|
| 156 |
-
|
| 157 |
-
if strings.HasPrefix(line, "Message ID (Canal):") {
|
| 158 |
-
if currentItem != nil {
|
| 159 |
-
idStr := strings.TrimSpace(strings.TrimPrefix(line, "Message ID (Canal):"))
|
| 160 |
-
id, err := strconv.Atoi(idStr)
|
| 161 |
-
if err == nil {
|
| 162 |
-
currentItem.MessageID = id
|
| 163 |
-
}
|
| 164 |
-
}
|
| 165 |
-
continue
|
| 166 |
-
}
|
| 167 |
-
}
|
| 168 |
-
if currentItem != nil && currentItem.MessageID != 0 {
|
| 169 |
-
items = append(items, *currentItem)
|
| 170 |
-
}
|
| 171 |
-
|
| 172 |
-
if len(items) == 0 {
|
| 173 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString("No se encontraron elementos válidos (Message ID) en el txt."), nil)
|
| 174 |
-
return nil
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
// Send processing message
|
| 178 |
-
loadingMsg, err := ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Procesando %d archivos... Por favor espera.", len(items))), nil)
|
| 179 |
-
|
| 180 |
-
channel, err := utils.GetLogChannelPeer(ctx.Context, ctx.Raw, ctx.PeerStorage)
|
| 181 |
-
if err != nil {
|
| 182 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Error obteniendo el canal de log: %v", err)), nil)
|
| 183 |
-
return err
|
| 184 |
-
}
|
| 185 |
-
|
| 186 |
-
for i := range items {
|
| 187 |
-
// Add small delay to avoid hitting FloodWait limit too fast
|
| 188 |
-
time.Sleep(200 * time.Millisecond)
|
| 189 |
-
|
| 190 |
-
inputMessageID := tg.InputMessageClass(&tg.InputMessageID{ID: items[i].MessageID})
|
| 191 |
-
messageRequest := tg.ChannelsGetMessagesRequest{Channel: channel, ID: []tg.InputMessageClass{inputMessageID}}
|
| 192 |
-
res, err := ctx.Raw.ChannelsGetMessages(ctx.Context, &messageRequest)
|
| 193 |
-
if err != nil {
|
| 194 |
-
utils.Logger.Error("Failed to fetch message", zap.Error(err))
|
| 195 |
-
continue
|
| 196 |
-
}
|
| 197 |
-
messages, ok := res.(*tg.MessagesChannelMessages)
|
| 198 |
-
if !ok || len(messages.Messages) == 0 {
|
| 199 |
-
utils.Logger.Error("Message not found or bad response")
|
| 200 |
-
continue
|
| 201 |
-
}
|
| 202 |
-
message, ok := messages.Messages[0].(*tg.Message)
|
| 203 |
-
if !ok {
|
| 204 |
-
utils.Logger.Error("Message cast failed")
|
| 205 |
-
continue
|
| 206 |
-
}
|
| 207 |
-
|
| 208 |
-
fileInfo, err := utils.FileFromMedia(message.Media)
|
| 209 |
-
if err != nil {
|
| 210 |
-
utils.Logger.Error("Failed to parse file media", zap.Error(err))
|
| 211 |
-
continue
|
| 212 |
-
}
|
| 213 |
-
if err != nil {
|
| 214 |
-
utils.Logger.Error("Failed to fetch message details for stream generation", zap.Int("messageId", items[i].MessageID), zap.Error(err))
|
| 215 |
-
continue
|
| 216 |
-
}
|
| 217 |
-
fullHash := utils.PackFile(
|
| 218 |
-
fileInfo.FileName,
|
| 219 |
-
fileInfo.FileSize,
|
| 220 |
-
fileInfo.MimeType,
|
| 221 |
-
fileInfo.ID,
|
| 222 |
-
)
|
| 223 |
-
hash := utils.GetShortHash(fullHash)
|
| 224 |
-
|
| 225 |
-
items[i].LocalLink = fmt.Sprintf("http://127.0.0.1:%d/stream/%d?hash=%s", config.ValueOf.Port, items[i].MessageID, hash)
|
| 226 |
-
items[i].HFLink = fmt.Sprintf("https://codebydante-brodyfilestreambot.hf.space/stream/%d?hash=%s", items[i].MessageID, hash)
|
| 227 |
-
}
|
| 228 |
-
|
| 229 |
-
// Generate Text Content Output
|
| 230 |
-
var webpItems []ParsedFileItem
|
| 231 |
-
var originalItems []ParsedFileItem
|
| 232 |
-
|
| 233 |
-
// Sort by name if desired, keeping original logic if preferred. User wants alphabetical "ordenando alfabeticamente" -> Yes we can do that!
|
| 234 |
-
for _, it := range items {
|
| 235 |
-
if it.LocalLink == "" {
|
| 236 |
-
continue // skip failed ones
|
| 237 |
-
}
|
| 238 |
-
if it.Type == "WEBP" {
|
| 239 |
-
webpItems = append(webpItems, it)
|
| 240 |
-
} else {
|
| 241 |
-
originalItems = append(originalItems, it)
|
| 242 |
-
}
|
| 243 |
-
}
|
| 244 |
-
|
| 245 |
-
sort.Slice(webpItems, func(i, j int) bool {
|
| 246 |
-
return webpItems[i].Name < webpItems[j].Name
|
| 247 |
-
})
|
| 248 |
-
sort.Slice(originalItems, func(i, j int) bool {
|
| 249 |
-
return originalItems[i].Name < originalItems[j].Name
|
| 250 |
-
})
|
| 251 |
-
|
| 252 |
-
var buf bytes.Buffer
|
| 253 |
-
buf.WriteString(fmt.Sprintf("==== %s ====\n\n", title))
|
| 254 |
-
|
| 255 |
-
if len(webpItems) > 0 {
|
| 256 |
-
buf.WriteString("---- WEB P LINKS ----\n\n")
|
| 257 |
-
buf.WriteString("🚀 Para Hugging Face (Público):\n")
|
| 258 |
-
for i, f := range webpItems {
|
| 259 |
-
buf.WriteString(fmt.Sprintf("(%d) %s = %s\n", i+1, f.Name, f.HFLink))
|
| 260 |
-
}
|
| 261 |
-
buf.WriteString("\n🖥 Para tu PC Local (Pruebas):\n")
|
| 262 |
-
for i, f := range webpItems {
|
| 263 |
-
buf.WriteString(fmt.Sprintf("(%d) %s = %s\n", i+1, f.Name, f.LocalLink))
|
| 264 |
-
}
|
| 265 |
-
buf.WriteString("\n----------------------------------\n\n")
|
| 266 |
-
}
|
| 267 |
-
|
| 268 |
-
if len(originalItems) > 0 {
|
| 269 |
-
buf.WriteString("---- ORIGINAL LINKS ----\n\n")
|
| 270 |
-
buf.WriteString("🚀 Para Hugging Face (Público):\n")
|
| 271 |
-
for i, f := range originalItems {
|
| 272 |
-
buf.WriteString(fmt.Sprintf("(%d) %s = %s\n", i+1, f.Name, f.HFLink))
|
| 273 |
-
}
|
| 274 |
-
buf.WriteString("\n🖥 Para tu PC Local (Pruebas):\n")
|
| 275 |
-
for i, f := range originalItems {
|
| 276 |
-
buf.WriteString(fmt.Sprintf("(%d) %s = %s\n", i+1, f.Name, f.LocalLink))
|
| 277 |
-
}
|
| 278 |
-
}
|
| 279 |
-
buf.WriteString("\n")
|
| 280 |
-
|
| 281 |
-
// Save and send back text
|
| 282 |
-
filename := title + ".txt"
|
| 283 |
-
if err := utils.UploadToTG(ctx, chatId, buf.Bytes(), filename); err != nil {
|
| 284 |
-
utils.Logger.Error("Failed to upload fileStream txt", zap.Error(err))
|
| 285 |
-
_, _ = ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Error enviando el archivo generado: %v", err)), nil)
|
| 286 |
-
}
|
| 287 |
-
|
| 288 |
-
// Delete loading msg
|
| 289 |
-
if loadingMsg != nil {
|
| 290 |
-
ctx.DeleteMessages(chatId, []int{loadingMsg.ID})
|
| 291 |
-
}
|
| 292 |
-
|
| 293 |
-
return nil
|
| 294 |
-
}
|
|
|
|
| 1 |
+
package commands
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bufio"
|
| 5 |
+
"bytes"
|
| 6 |
+
"fmt"
|
| 7 |
+
"sort"
|
| 8 |
+
"strconv"
|
| 9 |
+
"strings"
|
| 10 |
+
"sync"
|
| 11 |
+
"time"
|
| 12 |
+
|
| 13 |
+
"EverythingSuckz/fsb/config"
|
| 14 |
+
"EverythingSuckz/fsb/internal/utils"
|
| 15 |
+
|
| 16 |
+
"github.com/celestix/gotgproto/dispatcher"
|
| 17 |
+
"github.com/celestix/gotgproto/ext"
|
| 18 |
+
"github.com/gotd/td/telegram/downloader"
|
| 19 |
+
"github.com/gotd/td/tg"
|
| 20 |
+
"go.uber.org/zap"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
type FileStreamSession struct {
|
| 24 |
+
Active bool
|
| 25 |
+
mu sync.Mutex
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
var (
|
| 29 |
+
fileStreamSessions = make(map[int64]*FileStreamSession)
|
| 30 |
+
fileStreamMu sync.Mutex
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
func ToggleFileStream(ctx *ext.Context, u *ext.Update) error {
|
| 34 |
+
chatId := u.EffectiveChat().GetID()
|
| 35 |
+
messageText := u.EffectiveMessage.Message.Message
|
| 36 |
+
args := strings.Split(messageText, " ")
|
| 37 |
+
cmd := strings.ToLower(args[0])
|
| 38 |
+
|
| 39 |
+
var state string
|
| 40 |
+
if strings.Contains(cmd, "on") {
|
| 41 |
+
state = "on"
|
| 42 |
+
} else if strings.Contains(cmd, "off") {
|
| 43 |
+
state = "off"
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
if state == "" {
|
| 47 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("Uso: /file_id_stream_on o /file_id_stream_off"), nil)
|
| 48 |
+
return dispatcher.EndGroups
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
fileStreamMu.Lock()
|
| 52 |
+
session, ok := fileStreamSessions[chatId]
|
| 53 |
+
if !ok {
|
| 54 |
+
session = &FileStreamSession{Active: false}
|
| 55 |
+
fileStreamSessions[chatId] = session
|
| 56 |
+
}
|
| 57 |
+
fileStreamMu.Unlock()
|
| 58 |
+
|
| 59 |
+
session.mu.Lock()
|
| 60 |
+
defer session.mu.Unlock()
|
| 61 |
+
|
| 62 |
+
if state == "on" {
|
| 63 |
+
if session.Active {
|
| 64 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("File Stream ya está activado."), nil)
|
| 65 |
+
return dispatcher.EndGroups
|
| 66 |
+
}
|
| 67 |
+
session.Active = true
|
| 68 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("✅ File Stream activado. Envía tu archivo .txt con File IDs."), nil)
|
| 69 |
+
return dispatcher.EndGroups
|
| 70 |
+
} else if state == "off" {
|
| 71 |
+
if !session.Active {
|
| 72 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("File Stream ya está desactivado."), nil)
|
| 73 |
+
return dispatcher.EndGroups
|
| 74 |
+
}
|
| 75 |
+
session.Active = false
|
| 76 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("❌ File Stream desactivado."), nil)
|
| 77 |
+
return dispatcher.EndGroups
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
return dispatcher.EndGroups
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
func GetFileStreamSession(chatId int64) *FileStreamSession {
|
| 84 |
+
fileStreamMu.Lock()
|
| 85 |
+
defer fileStreamMu.Unlock()
|
| 86 |
+
return fileStreamSessions[chatId]
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
type ParsedFileItem struct {
|
| 90 |
+
Type string // "ORIGINAL" or "WEBP"
|
| 91 |
+
Name string
|
| 92 |
+
MessageID int
|
| 93 |
+
HFLink string
|
| 94 |
+
LocalLink string
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
func HandleFileStreamFile(chatId int64, ctx *ext.Context, u *ext.Update, fileMedia interface{}) error {
|
| 98 |
+
fileDoc, err := utils.FileFromMedia(fileMedia.(tg.MessageMediaClass))
|
| 99 |
+
if err != nil {
|
| 100 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Error obteniendo media de telegram: %v", err)), nil)
|
| 101 |
+
return err
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
// Download the .txt file
|
| 105 |
+
d := downloader.NewDownloader()
|
| 106 |
+
var docBuf bytes.Buffer
|
| 107 |
+
_, err = d.Download(ctx.Raw, fileDoc.Location).Stream(ctx.Context, &docBuf)
|
| 108 |
+
if err != nil {
|
| 109 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Error descargando el archivo: %v", err)), nil)
|
| 110 |
+
return err
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
fileBytes := docBuf.Bytes()
|
| 114 |
+
|
| 115 |
+
// Parse the .txt content
|
| 116 |
+
scanner := bufio.NewScanner(bytes.NewReader(fileBytes))
|
| 117 |
+
var title string = "FileStream_Collection"
|
| 118 |
+
var currentItem *ParsedFileItem
|
| 119 |
+
var items []ParsedFileItem
|
| 120 |
+
|
| 121 |
+
for scanner.Scan() {
|
| 122 |
+
line := strings.TrimSpace(scanner.Text())
|
| 123 |
+
if line == "" || strings.HasPrefix(line, "--") {
|
| 124 |
+
continue
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
if strings.HasPrefix(line, "=== Datos del ZIP: ") {
|
| 128 |
+
titleRaw := strings.TrimPrefix(line, "=== Datos del ZIP: ")
|
| 129 |
+
titleRaw = strings.TrimSuffix(titleRaw, " ===")
|
| 130 |
+
title = strings.TrimSuffix(titleRaw, ".zip")
|
| 131 |
+
title = strings.TrimSpace(title)
|
| 132 |
+
continue
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
if strings.HasPrefix(line, "[ORIGINAL]") {
|
| 136 |
+
if currentItem != nil && currentItem.MessageID != 0 {
|
| 137 |
+
items = append(items, *currentItem)
|
| 138 |
+
}
|
| 139 |
+
currentItem = &ParsedFileItem{
|
| 140 |
+
Type: "ORIGINAL",
|
| 141 |
+
Name: strings.TrimSpace(strings.TrimPrefix(line, "[ORIGINAL]")),
|
| 142 |
+
}
|
| 143 |
+
continue
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
if strings.HasPrefix(line, "[WEBP]") {
|
| 147 |
+
if currentItem != nil && currentItem.MessageID != 0 {
|
| 148 |
+
items = append(items, *currentItem)
|
| 149 |
+
}
|
| 150 |
+
currentItem = &ParsedFileItem{
|
| 151 |
+
Type: "WEBP",
|
| 152 |
+
Name: strings.TrimSpace(strings.TrimPrefix(line, "[WEBP]")),
|
| 153 |
+
}
|
| 154 |
+
continue
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
if strings.HasPrefix(line, "Message ID (Canal):") {
|
| 158 |
+
if currentItem != nil {
|
| 159 |
+
idStr := strings.TrimSpace(strings.TrimPrefix(line, "Message ID (Canal):"))
|
| 160 |
+
id, err := strconv.Atoi(idStr)
|
| 161 |
+
if err == nil {
|
| 162 |
+
currentItem.MessageID = id
|
| 163 |
+
}
|
| 164 |
+
}
|
| 165 |
+
continue
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
if currentItem != nil && currentItem.MessageID != 0 {
|
| 169 |
+
items = append(items, *currentItem)
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
if len(items) == 0 {
|
| 173 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString("No se encontraron elementos válidos (Message ID) en el txt."), nil)
|
| 174 |
+
return nil
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
// Send processing message
|
| 178 |
+
loadingMsg, err := ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Procesando %d archivos... Por favor espera.", len(items))), nil)
|
| 179 |
+
|
| 180 |
+
channel, err := utils.GetLogChannelPeer(ctx.Context, ctx.Raw, ctx.PeerStorage)
|
| 181 |
+
if err != nil {
|
| 182 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Error obteniendo el canal de log: %v", err)), nil)
|
| 183 |
+
return err
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
for i := range items {
|
| 187 |
+
// Add small delay to avoid hitting FloodWait limit too fast
|
| 188 |
+
time.Sleep(200 * time.Millisecond)
|
| 189 |
+
|
| 190 |
+
inputMessageID := tg.InputMessageClass(&tg.InputMessageID{ID: items[i].MessageID})
|
| 191 |
+
messageRequest := tg.ChannelsGetMessagesRequest{Channel: channel, ID: []tg.InputMessageClass{inputMessageID}}
|
| 192 |
+
res, err := ctx.Raw.ChannelsGetMessages(ctx.Context, &messageRequest)
|
| 193 |
+
if err != nil {
|
| 194 |
+
utils.Logger.Error("Failed to fetch message", zap.Error(err))
|
| 195 |
+
continue
|
| 196 |
+
}
|
| 197 |
+
messages, ok := res.(*tg.MessagesChannelMessages)
|
| 198 |
+
if !ok || len(messages.Messages) == 0 {
|
| 199 |
+
utils.Logger.Error("Message not found or bad response")
|
| 200 |
+
continue
|
| 201 |
+
}
|
| 202 |
+
message, ok := messages.Messages[0].(*tg.Message)
|
| 203 |
+
if !ok {
|
| 204 |
+
utils.Logger.Error("Message cast failed")
|
| 205 |
+
continue
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
fileInfo, err := utils.FileFromMedia(message.Media)
|
| 209 |
+
if err != nil {
|
| 210 |
+
utils.Logger.Error("Failed to parse file media", zap.Error(err))
|
| 211 |
+
continue
|
| 212 |
+
}
|
| 213 |
+
if err != nil {
|
| 214 |
+
utils.Logger.Error("Failed to fetch message details for stream generation", zap.Int("messageId", items[i].MessageID), zap.Error(err))
|
| 215 |
+
continue
|
| 216 |
+
}
|
| 217 |
+
fullHash := utils.PackFile(
|
| 218 |
+
fileInfo.FileName,
|
| 219 |
+
fileInfo.FileSize,
|
| 220 |
+
fileInfo.MimeType,
|
| 221 |
+
fileInfo.ID,
|
| 222 |
+
)
|
| 223 |
+
hash := utils.GetShortHash(fullHash)
|
| 224 |
+
|
| 225 |
+
items[i].LocalLink = fmt.Sprintf("http://127.0.0.1:%d/stream/%d?hash=%s", config.ValueOf.Port, items[i].MessageID, hash)
|
| 226 |
+
items[i].HFLink = fmt.Sprintf("https://codebydante-brodyfilestreambot.hf.space/stream/%d?hash=%s", items[i].MessageID, hash)
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
// Generate Text Content Output
|
| 230 |
+
var webpItems []ParsedFileItem
|
| 231 |
+
var originalItems []ParsedFileItem
|
| 232 |
+
|
| 233 |
+
// Sort by name if desired, keeping original logic if preferred. User wants alphabetical "ordenando alfabeticamente" -> Yes we can do that!
|
| 234 |
+
for _, it := range items {
|
| 235 |
+
if it.LocalLink == "" {
|
| 236 |
+
continue // skip failed ones
|
| 237 |
+
}
|
| 238 |
+
if it.Type == "WEBP" {
|
| 239 |
+
webpItems = append(webpItems, it)
|
| 240 |
+
} else {
|
| 241 |
+
originalItems = append(originalItems, it)
|
| 242 |
+
}
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
sort.Slice(webpItems, func(i, j int) bool {
|
| 246 |
+
return webpItems[i].Name < webpItems[j].Name
|
| 247 |
+
})
|
| 248 |
+
sort.Slice(originalItems, func(i, j int) bool {
|
| 249 |
+
return originalItems[i].Name < originalItems[j].Name
|
| 250 |
+
})
|
| 251 |
+
|
| 252 |
+
var buf bytes.Buffer
|
| 253 |
+
buf.WriteString(fmt.Sprintf("==== %s ====\n\n", title))
|
| 254 |
+
|
| 255 |
+
if len(webpItems) > 0 {
|
| 256 |
+
buf.WriteString("---- WEB P LINKS ----\n\n")
|
| 257 |
+
buf.WriteString("🚀 Para Hugging Face (Público):\n")
|
| 258 |
+
for i, f := range webpItems {
|
| 259 |
+
buf.WriteString(fmt.Sprintf("(%d) %s = %s\n", i+1, f.Name, f.HFLink))
|
| 260 |
+
}
|
| 261 |
+
buf.WriteString("\n🖥 Para tu PC Local (Pruebas):\n")
|
| 262 |
+
for i, f := range webpItems {
|
| 263 |
+
buf.WriteString(fmt.Sprintf("(%d) %s = %s\n", i+1, f.Name, f.LocalLink))
|
| 264 |
+
}
|
| 265 |
+
buf.WriteString("\n----------------------------------\n\n")
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
if len(originalItems) > 0 {
|
| 269 |
+
buf.WriteString("---- ORIGINAL LINKS ----\n\n")
|
| 270 |
+
buf.WriteString("🚀 Para Hugging Face (Público):\n")
|
| 271 |
+
for i, f := range originalItems {
|
| 272 |
+
buf.WriteString(fmt.Sprintf("(%d) %s = %s\n", i+1, f.Name, f.HFLink))
|
| 273 |
+
}
|
| 274 |
+
buf.WriteString("\n🖥 Para tu PC Local (Pruebas):\n")
|
| 275 |
+
for i, f := range originalItems {
|
| 276 |
+
buf.WriteString(fmt.Sprintf("(%d) %s = %s\n", i+1, f.Name, f.LocalLink))
|
| 277 |
+
}
|
| 278 |
+
}
|
| 279 |
+
buf.WriteString("\n")
|
| 280 |
+
|
| 281 |
+
// Save and send back text
|
| 282 |
+
filename := title + ".txt"
|
| 283 |
+
if err := utils.UploadToTG(ctx, chatId, buf.Bytes(), filename); err != nil {
|
| 284 |
+
utils.Logger.Error("Failed to upload fileStream txt", zap.Error(err))
|
| 285 |
+
_, _ = ctx.Reply(u, ext.ReplyTextString(fmt.Sprintf("Error enviando el archivo generado: %v", err)), nil)
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
// Delete loading msg
|
| 289 |
+
if loadingMsg != nil {
|
| 290 |
+
ctx.DeleteMessages(chatId, []int{loadingMsg.ID})
|
| 291 |
+
}
|
| 292 |
+
|
| 293 |
+
return nil
|
| 294 |
+
}
|
internal/routes/stream.go
CHANGED
|
@@ -2,16 +2,17 @@ package routes
|
|
| 2 |
|
| 3 |
import (
|
| 4 |
"EverythingSuckz/fsb/internal/bot"
|
|
|
|
| 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"
|
|
@@ -67,81 +68,61 @@ func getStreamRoute(ctx *gin.Context) {
|
|
| 67 |
return
|
| 68 |
}
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 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)
|
| 90 |
}
|
| 91 |
-
return
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
ctx.Header("Accept-Ranges", "bytes")
|
| 95 |
-
var start, end int64
|
| 96 |
-
rangeHeader := r.Header.Get("Range")
|
| 97 |
|
| 98 |
-
|
| 99 |
-
start = 0
|
| 100 |
-
end = file.FileSize - 1
|
| 101 |
-
w.WriteHeader(http.StatusOK)
|
| 102 |
-
} else {
|
| 103 |
-
ranges, err := range_parser.Parse(file.FileSize, r.Header.Get("Range"))
|
| 104 |
if err != nil {
|
| 105 |
-
|
| 106 |
-
return
|
| 107 |
}
|
| 108 |
-
|
| 109 |
-
end = ranges[0].End
|
| 110 |
-
ctx.Header("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, file.FileSize))
|
| 111 |
-
log.Info("Content-Range", zap.Int64("start", start), zap.Int64("end", end), zap.Int64("fileSize", file.FileSize))
|
| 112 |
-
w.WriteHeader(http.StatusPartialContent)
|
| 113 |
}
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
| 120 |
}
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
disposition := "inline"
|
| 126 |
-
|
| 127 |
if ctx.Query("d") == "true" {
|
| 128 |
disposition = "attachment"
|
| 129 |
}
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 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))
|
| 144 |
-
}
|
| 145 |
}
|
|
|
|
| 146 |
}
|
|
|
|
|
|
|
| 147 |
}
|
|
|
|
| 2 |
|
| 3 |
import (
|
| 4 |
"EverythingSuckz/fsb/internal/bot"
|
| 5 |
+
"EverythingSuckz/fsb/internal/cache"
|
| 6 |
"EverythingSuckz/fsb/internal/stream"
|
| 7 |
"EverythingSuckz/fsb/internal/types"
|
| 8 |
"EverythingSuckz/fsb/internal/utils"
|
| 9 |
+
"bytes"
|
| 10 |
"fmt"
|
| 11 |
"io"
|
| 12 |
"net/http"
|
| 13 |
"strconv"
|
| 14 |
|
| 15 |
"github.com/gotd/td/tg"
|
|
|
|
| 16 |
"go.uber.org/zap"
|
| 17 |
|
| 18 |
"github.com/gin-gonic/gin"
|
|
|
|
| 68 |
return
|
| 69 |
}
|
| 70 |
|
| 71 |
+
format := ctx.Query("format")
|
| 72 |
+
fetcherFunc := func() (io.ReadCloser, error) {
|
| 73 |
+
if file.FileSize == 0 {
|
| 74 |
+
res, err := worker.Client.API().UploadGetFile(reqCtx, &tg.UploadGetFileRequest{
|
| 75 |
+
Location: file.Location,
|
| 76 |
+
Offset: 0,
|
| 77 |
+
Limit: 1024 * 1024,
|
| 78 |
+
})
|
| 79 |
+
if err != nil {
|
| 80 |
+
return nil, err
|
| 81 |
+
}
|
| 82 |
+
result, ok := res.(*tg.UploadFile)
|
| 83 |
+
if !ok {
|
| 84 |
+
return nil, fmt.Errorf("unexpected response")
|
| 85 |
+
}
|
| 86 |
+
return io.NopCloser(bytes.NewReader(result.GetBytes())), nil
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
pipe, err := stream.NewStreamPipe(reqCtx, worker.Client, file.Location, 0, file.FileSize-1, log)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
if err != nil {
|
| 91 |
+
return nil, err
|
|
|
|
| 92 |
}
|
| 93 |
+
return pipe, nil
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
+
var cachedPath string
|
| 97 |
+
var errCache error
|
| 98 |
+
if format == "webp" {
|
| 99 |
+
cachedPath, errCache = cache.Disk.GetOrConvertWebP(reqCtx, messageID, fetcherFunc)
|
| 100 |
+
} else {
|
| 101 |
+
cachedPath, errCache = cache.Disk.GetOrDownload(reqCtx, messageID, fetcherFunc)
|
| 102 |
}
|
| 103 |
|
| 104 |
+
if errCache != nil {
|
| 105 |
+
log.Error("Failed to fetch or convert file", zap.Error(errCache))
|
| 106 |
+
http.Error(w, errCache.Error(), http.StatusInternalServerError)
|
| 107 |
+
return
|
| 108 |
+
}
|
| 109 |
|
| 110 |
disposition := "inline"
|
|
|
|
| 111 |
if ctx.Query("d") == "true" {
|
| 112 |
disposition = "attachment"
|
| 113 |
}
|
| 114 |
|
| 115 |
+
if format == "webp" {
|
| 116 |
+
ctx.Header("Content-Disposition", fmt.Sprintf("%s; filename=\"%s.webp\"", disposition, messageIDParm))
|
| 117 |
+
ctx.Header("Content-Type", "image/webp")
|
| 118 |
+
} else {
|
| 119 |
+
ctx.Header("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, file.FileName))
|
| 120 |
+
mimeType := file.MimeType
|
| 121 |
+
if mimeType == "" {
|
| 122 |
+
mimeType = "application/octet-stream"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
}
|
| 124 |
+
ctx.Header("Content-Type", mimeType)
|
| 125 |
}
|
| 126 |
+
|
| 127 |
+
http.ServeFile(w, r, cachedPath)
|
| 128 |
}
|