@@ -24,6 +24,7 @@ import (
24
24
"github.com/readium/go-toolkit/pkg/manifest"
25
25
"github.com/readium/go-toolkit/pkg/pub"
26
26
"github.com/readium/go-toolkit/pkg/streamer"
27
+ "github.com/readium/go-toolkit/pkg/util/url"
27
28
"github.com/zeebo/xxh3"
28
29
)
29
30
@@ -67,32 +68,6 @@ func (s *Server) getPublication(filename string) (*pub.Publication, error) {
67
68
return nil , errors .Wrap (err , "failed opening " + cp )
68
69
}
69
70
70
- // TODO: Remove this after we make links relative in the go-toolkit
71
- for i , link := range pub .Manifest .Links {
72
- pub .Manifest .Links [i ] = makeRelative (link )
73
- }
74
- for i , link := range pub .Manifest .Resources {
75
- pub .Manifest .Resources [i ] = makeRelative (link )
76
- }
77
- for i , link := range pub .Manifest .ReadingOrder {
78
- pub .Manifest .ReadingOrder [i ] = makeRelative (link )
79
- }
80
- for i , link := range pub .Manifest .TableOfContents {
81
- pub .Manifest .TableOfContents [i ] = makeRelative (link )
82
- }
83
- var makeCollectionRelative func (mp manifest.PublicationCollectionMap )
84
- makeCollectionRelative = func (mp manifest.PublicationCollectionMap ) {
85
- for i := range mp {
86
- for j := range mp [i ] {
87
- for k := range mp [i ][j ].Links {
88
- mp [i ][j ].Links [k ] = makeRelative (mp [i ][j ].Links [k ])
89
- }
90
- makeCollectionRelative (mp [i ][j ].Subcollections )
91
- }
92
- }
93
- }
94
- makeCollectionRelative (pub .Manifest .Subcollections )
95
-
96
71
// Cache the publication
97
72
encPub := & cache.CachedPublication {Publication : pub }
98
73
s .lfu .Set (cp , encPub )
@@ -122,10 +97,19 @@ func (s *Server) getManifest(w http.ResponseWriter, req *http.Request) {
122
97
scheme = "https://"
123
98
}
124
99
rPath , _ := s .router .Get ("manifest" ).URLPath ("path" , vars ["path" ])
100
+ conformsTo := conformsToAsMimetype (publication .Manifest .Metadata .ConformsTo )
101
+
102
+ selfUrl , err := url .AbsoluteURLFromString (scheme + req .Host + rPath .String ())
103
+ if err != nil {
104
+ slog .Error ("failed creating self URL" , "error" , err )
105
+ w .WriteHeader (500 )
106
+ return
107
+ }
108
+
125
109
selfLink := & manifest.Link {
126
- Rels : manifest.Strings {"self" },
127
- Type : conformsToAsMimetype ( publication . Manifest . Metadata . ConformsTo ) ,
128
- Href : scheme + req . Host + rPath . String ( ),
110
+ Rels : manifest.Strings {"self" },
111
+ MediaType : & conformsTo ,
112
+ Href : manifest . NewHREF ( selfUrl ),
129
113
}
130
114
131
115
// Marshal the manifest
@@ -155,7 +139,7 @@ func (s *Server) getManifest(w http.ResponseWriter, req *http.Request) {
155
139
}
156
140
157
141
// Add headers
158
- w .Header ().Set ("content-type" , conformsToAsMimetype ( publication . Manifest . Metadata . ConformsTo )+ "; charset=utf-8" )
142
+ w .Header ().Set ("content-type" , conformsTo . String ( )+ "; charset=utf-8" )
159
143
w .Header ().Set ("cache-control" , "private, must-revalidate" )
160
144
w .Header ().Set ("access-control-allow-origin" , "*" ) // TODO: provide options?
161
145
@@ -190,18 +174,28 @@ func (s *Server) getAsset(w http.ResponseWriter, r *http.Request) {
190
174
return
191
175
}
192
176
177
+ // Parse asset path from mux vars
178
+ href , err := url .URLFromDecodedPath (path .Clean (vars ["asset" ]))
179
+ if err != nil {
180
+ slog .Error ("failed parsing asset path as URL" , "error" , err )
181
+ w .WriteHeader (400 )
182
+ return
183
+ }
184
+ rawHref := href .Raw ()
185
+ rawHref .RawQuery = r .URL .Query ().Encode () // Add the query parameters of the URL
186
+ href , _ = url .RelativeURLFromGo (rawHref ) // Turn it back into a go-toolkit relative URL
187
+
193
188
// Make sure the asset exists in the publication
194
- href := path .Clean (vars ["asset" ])
195
- link := publication .Find (href )
189
+ link := publication .LinkWithHref (href )
196
190
if link == nil {
197
191
w .WriteHeader (http .StatusNotFound )
198
192
return
199
193
}
200
194
finalLink := * link
201
195
202
196
// Expand templated links to include URL query parameters
203
- if finalLink .Templated {
204
- finalLink = finalLink .ExpandTemplate ( convertURLValuesToMap (r .URL .Query ()))
197
+ if finalLink .Href . IsTemplated () {
198
+ finalLink . Href = manifest . NewHREF ( finalLink .URL ( nil , convertURLValuesToMap (r .URL .Query () )))
205
199
}
206
200
207
201
// Get the asset from the publication
@@ -217,7 +211,7 @@ func (s *Server) getAsset(w http.ResponseWriter, r *http.Request) {
217
211
}
218
212
219
213
// Patch mimetype where necessary
220
- contentType := link .MediaType () .String ()
214
+ contentType := link .MediaType .String ()
221
215
if sub , ok := mimeSubstitutions [contentType ]; ok {
222
216
contentType = sub
223
217
}
0 commit comments