-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgino
More file actions
175 lines (149 loc) · 5.02 KB
/
gino
File metadata and controls
175 lines (149 loc) · 5.02 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
go run main.go -port=8080 -download-dir=./mydownloads
-------------------------------------------------------------------------------------------------------------
go get github.com/anacrolix/torrent
go get github.com/gin-gonic/gin
-------------------------------------------------------------------------------------------------------------
using gin framework and anacrolix
Torrent to http streaming gino app , example endpoint http://side.com/stream?torrent=
---------------------------------------------------------------------------------------------------------------
package main
import (
"flag"
"fmt"
"github.com/anacrolix/torrent"
"github.com/gin-gonic/gin"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
)
var clientPool = sync.Pool{
New: func() interface{} {
clientConfig := torrent.NewDefaultClientConfig()
clientConfig.ListenPort = 0 // Use a random available port
client, err := torrent.NewClient(clientConfig)
if err != nil {
log.Fatalf("Failed to create torrent client: %s", err)
}
return client
},
}
var metadataCache = make(map[string]*torrent.Torrent)
var cacheMutex sync.Mutex
func main() {
// Parse command-line arguments
port := flag.String("port", "8080", "Port to run the server on")
downloadDir := flag.String("download-dir", "./downloads", "Directory to store downloaded files")
flag.Parse()
// Create the download directory if it doesn't exist
if err := os.MkdirAll(*downloadDir, os.ModePerm); err != nil {
log.Fatalf("Failed to create download directory: %s", err)
}
// Set the download directory for the torrent client
clientConfig := torrent.NewDefaultClientConfig()
clientConfig.DataDir = *downloadDir
clientConfig.ListenPort = 0 // Use a random available port
// Initialize the torrent client pool with the custom configuration
clientPool = sync.Pool{
New: func() interface{} {
client, err := torrent.NewClient(clientConfig)
if err != nil {
log.Fatalf("Failed to create torrent client: %s", err)
}
return client
},
}
r := gin.Default()
r.GET("/stream", streamTorrent)
// Start HTTP/1.1 server
if err := r.Run(":" + *port); err != nil {
log.Fatalf("Failed to start HTTP/1.1 server: %s", err)
}
}
func streamTorrent(c *gin.Context) {
magnetURI := c.Query("torrent")
if magnetURI == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Magnet URI is required"})
return
}
// Get a torrent client from the pool
client := clientPool.Get().(*torrent.Client)
defer clientPool.Put(client)
// Check if metadata is already cached
var t *torrent.Torrent
cacheMutex.Lock()
t, ok := metadataCache[magnetURI]
cacheMutex.Unlock()
if !ok {
var err error
t, err = client.AddMagnet(magnetURI)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to add torrent"})
return
}
<-t.GotInfo()
// Cache the metadata
cacheMutex.Lock()
metadataCache[magnetURI] = t
cacheMutex.Unlock()
}
file := t.Files()[0]
reader := file.NewReader()
defer reader.Close()
// Determine the content type based on the file extension
contentType := "video/mp4"
if strings.HasSuffix(file.Path(), ".mkv") {
contentType = "video/x-matroska"
}
c.Header("Content-Type", contentType)
c.Header("Content-Disposition", "inline; filename="+filepath.Base(file.Path()))
c.Header("Accept-Ranges", "bytes")
// Handle Range requests for seeking and segmented streaming
rangeHeader := c.GetHeader("Range")
if rangeHeader != "" {
parts := strings.Split(strings.Replace(rangeHeader, "bytes=", "", 1), "-")
start, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid range header"})
return
}
end := int64(file.Length()) - 1
if len(parts) > 1 && parts[1] != "" {
end, err = strconv.ParseInt(parts[1], 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid range header"})
return
}
}
if start >= 0 && start < end && end < int64(file.Length()) {
_, err := reader.Seek(start, io.SeekStart)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to seek file"})
return
}
c.Header("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, file.Length()))
c.Header("Content-Length", strconv.FormatInt(end-start+1, 10))
c.Status(http.StatusPartialContent)
} else {
c.Header("Content-Length", strconv.FormatInt(file.Length(), 10))
c.Status(http.StatusOK)
}
} else {
c.Header("Content-Length", strconv.FormatInt(file.Length(), 10))
c.Status(http.StatusOK)
}
// Use io.CopyBuffer for efficient I/O operations
const bufferSize = 1 * 1024 * 1024 // 1MB
buffer := make([]byte, bufferSize)
// Copy data from the reader to the response writer
_, err := io.CopyBuffer(c.Writer, reader, buffer)
if err != nil && err != io.EOF && !strings.Contains(err.Error(), "write: broken pipe") {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to stream file"})
return
}
}
---------------------------------------------------------------------------------------------------------------