2020-06-14 00:09:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-23 13:08:53 +00:00
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2020-06-14 00:09:06 +00:00
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"time"
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
"github.com/Luzifer/preserve/pkg/storage"
|
2020-06-14 00:09:06 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-12-29 16:58:30 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-06-14 00:09:06 +00:00
|
|
|
)
|
|
|
|
|
2021-11-05 16:26:15 +00:00
|
|
|
const lastSuccessStatus = 299
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
func renewCache(ctx context.Context, url string) (*storage.Meta, error) {
|
2021-02-23 13:08:53 +00:00
|
|
|
cachePath := urlToCachePath(url)
|
2020-06-14 00:09:06 +00:00
|
|
|
|
2021-11-05 16:26:15 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
2020-06-14 01:04:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to create request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.UserAgent != "" {
|
|
|
|
req.Header.Set("User-Agent", cfg.UserAgent)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2020-06-14 00:09:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "Unable to fetch source file")
|
|
|
|
}
|
2023-12-29 16:58:30 +00:00
|
|
|
defer func() {
|
|
|
|
if err := resp.Body.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing response body (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2020-06-14 00:09:06 +00:00
|
|
|
|
2021-11-05 16:26:15 +00:00
|
|
|
if resp.StatusCode > lastSuccessStatus {
|
2020-06-14 00:09:06 +00:00
|
|
|
return nil, errors.Errorf("HTTP status signaled failure: %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:08:53 +00:00
|
|
|
lm := time.Now()
|
2020-06-14 00:09:06 +00:00
|
|
|
if t, err := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified")); err == nil {
|
|
|
|
lm = t
|
|
|
|
}
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
metadata := &storage.Meta{
|
2020-06-14 00:09:06 +00:00
|
|
|
ContentType: resp.Header.Get("Content-Type"),
|
|
|
|
LastCached: time.Now(),
|
|
|
|
LastModified: lm,
|
|
|
|
}
|
|
|
|
|
2023-12-29 16:58:30 +00:00
|
|
|
if err := store.StoreFile(ctx, cachePath, metadata, resp.Body); err != nil {
|
|
|
|
return nil, fmt.Errorf("storing file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata, nil
|
2021-02-23 13:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func urlToCachePath(url string) string {
|
|
|
|
h := fmt.Sprintf("%x", sha256.Sum256([]byte(url)))
|
|
|
|
return path.Join(h[0:2], h)
|
2020-06-14 00:09:06 +00:00
|
|
|
}
|