77 "fmt"
88 "log/slog"
99 "net/http"
10+ "strings"
1011 "sync"
1112 "time"
1213
@@ -19,6 +20,7 @@ import (
1920 "github.com/chaitin/MonkeyCode/backend/errcode"
2021 "github.com/chaitin/MonkeyCode/backend/internal/middleware"
2122 "github.com/chaitin/MonkeyCode/backend/pkg/session"
23+ "github.com/chaitin/MonkeyCode/backend/pkg/version"
2224 "github.com/chaitin/MonkeyCode/backend/pkg/vsix"
2325)
2426
@@ -173,26 +175,22 @@ func (h *UserHandler) VSIXDownload(c *web.Context) error {
173175 return c .String (http .StatusTooManyRequests , "Too Many Requests" )
174176 }
175177
176- v , err := h .euse .GetByVersion (c .Request ().Context (), c .Param ("version" ))
177- if err != nil {
178- return err
179- }
180-
181178 s , err := h .usecase .GetSetting (c .Request ().Context ())
182179 if err != nil {
183180 return err
184181 }
185182
186183 host := c .Request ().Host
187184 h .logger .With ("url" , c .Request ().URL ).With ("header" , c .Request ().Header ).With ("host" , host ).DebugContext (c .Request ().Context (), "vsix download" )
188- cacheKey := h .generateCacheKey (v .Version , h .cfg .GetBaseURL (c .Request (), s ))
185+ cacheKey := h .generateCacheKey (version .Version , h .cfg .GetBaseURL (c .Request (), s ))
186+ version := strings .Trim (version .Version , "v" )
189187
190188 h .cacheMu .RLock ()
191189 if entry , exists := h .vsixCache [cacheKey ]; exists {
192190 if time .Since (entry .createdAt ) < time .Hour {
193191 h .cacheMu .RUnlock ()
194192
195- disposition := fmt .Sprintf ("attachment; filename=monkeycode-%s.vsix" , v . Version )
193+ disposition := fmt .Sprintf ("attachment; filename=monkeycode-%s.vsix" , version )
196194 c .Response ().Header ().Set ("Content-Type" , "application/octet-stream" )
197195 c .Response ().Header ().Set ("Content-Disposition" , disposition )
198196 c .Response ().Header ().Set ("Content-Length" , fmt .Sprintf ("%d" , len (entry .data )))
@@ -204,7 +202,7 @@ func (h *UserHandler) VSIXDownload(c *web.Context) error {
204202 h .cacheMu .RUnlock ()
205203
206204 var buf bytes.Buffer
207- if err := vsix .ChangeVsixEndpoint (v . Path , "extension/package.json" , h .cfg .GetBaseURL (c .Request (), s ), & buf ); err != nil {
205+ if err := vsix .ChangeVsixEndpoint (fmt . Sprintf ( "/app/assets/vsix/monkeycode-%s.vsix" , version ) , "extension/package.json" , h .cfg .GetBaseURL (c .Request (), s ), & buf ); err != nil {
208206 return err
209207 }
210208
@@ -218,7 +216,7 @@ func (h *UserHandler) VSIXDownload(c *web.Context) error {
218216
219217 go h .cleanExpiredCache ()
220218
221- disposition := fmt .Sprintf ("attachment; filename=monkeycode-%s.vsix" , v . Version )
219+ disposition := fmt .Sprintf ("attachment; filename=monkeycode-%s.vsix" , version )
222220 c .Response ().Header ().Set ("Content-Type" , "application/octet-stream" )
223221 c .Response ().Header ().Set ("Content-Disposition" , disposition )
224222 c .Response ().Header ().Set ("Content-Length" , fmt .Sprintf ("%d" , len (data )))
0 commit comments