@@ -3,32 +3,50 @@ package server
33import (
44 "bytes"
55 "net/http"
6+ "path/filepath"
67 "time"
78
9+ "github.com/gorilla/mux"
10+
811 "github.com/replicate/cog/pkg/console"
12+ "github.com/replicate/cog/pkg/storage"
913)
1014
1115func (s * Server ) DownloadModel (w http.ResponseWriter , r * http.Request ) {
1216 user , name , id := getRepoVars (r )
1317 modTime := time .Now () // TODO
1418
15- mod , err := s .db . GetModel (user , name , id )
19+ content , err := s .store . Download (user , name , id )
1620 if err != nil {
17- console . Error ( err . Error ())
18- w .WriteHeader (http .StatusInternalServerError )
19- return
20- }
21- if mod == nil {
22- w . WriteHeader ( http . StatusNotFound )
21+ if err == storage . NotFound {
22+ w .WriteHeader (http .StatusNotFound )
23+ } else {
24+ console . Error ( err . Error ())
25+ w . WriteHeader ( http . StatusInternalServerError )
26+ }
2327 return
2428 }
29+ console .Infof ("Downloaded %d bytes" , len (content ))
30+ http .ServeContent (w , r , id + ".zip" , modTime , bytes .NewReader (content ))
31+ }
2532
26- content , err := s .store .Download (user , name , id )
33+ func (s * Server ) DownloadFile (w http.ResponseWriter , r * http.Request ) {
34+ user , name , id := getRepoVars (r )
35+ vars := mux .Vars (r )
36+ path := vars ["path" ]
37+ modTime := time .Now () // TODO
38+
39+ content , err := s .store .DownloadFile (user , name , id , path )
2740 if err != nil {
28- console .Error (err .Error ())
29- w .WriteHeader (http .StatusInternalServerError )
41+ if err == storage .NotFound {
42+ w .WriteHeader (http .StatusNotFound )
43+ } else {
44+ console .Error (err .Error ())
45+ w .WriteHeader (http .StatusInternalServerError )
46+ }
3047 return
3148 }
49+ filename := filepath .Base (path )
3250 console .Infof ("Downloaded %d bytes" , len (content ))
33- http .ServeContent (w , r , id + ".zip" , modTime , bytes .NewReader (content ))
51+ http .ServeContent (w , r , filename , modTime , bytes .NewReader (content ))
3452}
0 commit comments