mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-18 18:39:42 +00:00
break into own func and fix
This commit is contained in:
parent
40478215d0
commit
4a2a14272b
1 changed files with 30 additions and 15 deletions
|
@ -153,12 +153,7 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
mimeType := client.getMimeTypeByExtension(resource)
|
mimeType := client.getMimeTypeByExtension(resource)
|
||||||
resp.Response.Header.Set(contentTypeHeader, mimeType)
|
resp.Response.Header.Set(contentTypeHeader, mimeType)
|
||||||
|
|
||||||
contentLeng, err2 := strconv.ParseInt(resp.Header.Get(contentLengthHeader), 20, 64)
|
if !shouldRespBeSavedToCache(resp.Response) {
|
||||||
if err2 != nil {
|
|
||||||
log.Error().Err(err2).Msg("could not parse content length")
|
|
||||||
}
|
|
||||||
if contentLeng <= 0 && contentLeng > fileCacheSizeLimit {
|
|
||||||
// if content to big or could not be determined we return now
|
|
||||||
return reader, resp.Response, err
|
return reader, resp.Response, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,8 +165,10 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
// TODO: cache is half-empty if request is cancelled - does the ctx.Err() below do the trick?
|
// TODO: cache is half-empty if request is cancelled - does the ctx.Err() below do the trick?
|
||||||
// err = res.BodyWriteTo(io.MultiWriter(ctx.Response().BodyWriter(), &cacheBodyWriter))
|
// err = res.BodyWriteTo(io.MultiWriter(ctx.Response().BodyWriter(), &cacheBodyWriter))
|
||||||
*/
|
*/
|
||||||
body, err := io.ReadAll(io.LimitReader(reader, contentLeng))
|
body, err := io.ReadAll(io.LimitReader(reader, fileCacheSizeLimit))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("not expected")
|
||||||
|
}
|
||||||
if err := client.responseCache.Set(cacheKey, FileResponse{
|
if err := client.responseCache.Set(cacheKey, FileResponse{
|
||||||
Exists: true,
|
Exists: true,
|
||||||
ETag: resp.Header.Get(eTagHeader),
|
ETag: resp.Header.Get(eTagHeader),
|
||||||
|
@ -180,7 +177,6 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
}, fileCacheTimeout); err != nil {
|
}, fileCacheTimeout); err != nil {
|
||||||
log.Error().Err(err).Msg("could not save content in cache")
|
log.Error().Err(err).Msg("could not save content in cache")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return io.NopCloser(bytes.NewReader(body)), resp.Response, nil
|
return io.NopCloser(bytes.NewReader(body)), resp.Response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,3 +255,22 @@ func (client *Client) getMimeTypeByExtension(resource string) string {
|
||||||
}
|
}
|
||||||
return mimeType
|
return mimeType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shouldRespBeSavedToCache(resp *http.Response) bool {
|
||||||
|
if resp == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
contentLengRaw := resp.Header.Get(contentLengthHeader)
|
||||||
|
if contentLengRaw == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
contentLeng, err := strconv.ParseInt(contentLengRaw, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("could not parse content length")
|
||||||
|
}
|
||||||
|
|
||||||
|
// if content to big or could not be determined we not cache it
|
||||||
|
return contentLeng > 0 && contentLeng < fileCacheSizeLimit
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue