2022-07-06 23:19:05 +02:00
|
|
|
package apis
|
|
|
|
|
|
|
|
import (
|
2023-12-04 16:40:35 +02:00
|
|
|
"context"
|
2023-04-04 19:33:35 +02:00
|
|
|
"errors"
|
2023-02-18 19:33:42 +02:00
|
|
|
"fmt"
|
2023-12-04 16:40:35 +02:00
|
|
|
"log/slog"
|
2023-04-04 19:33:35 +02:00
|
|
|
"net/http"
|
2023-12-04 16:40:35 +02:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2023-02-18 19:33:42 +02:00
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
"github.com/pocketbase/pocketbase/core"
|
2023-12-04 16:40:35 +02:00
|
|
|
"github.com/pocketbase/pocketbase/tools/filesystem"
|
2022-07-06 23:19:05 +02:00
|
|
|
"github.com/pocketbase/pocketbase/tools/list"
|
2024-09-29 18:23:19 +02:00
|
|
|
"github.com/pocketbase/pocketbase/tools/router"
|
2023-12-04 16:40:35 +02:00
|
|
|
"golang.org/x/sync/semaphore"
|
|
|
|
"golang.org/x/sync/singleflight"
|
2022-07-06 23:19:05 +02:00
|
|
|
)
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
var imageContentTypes = []string{"image/png", "image/jpg", "image/jpeg", "image/gif"}
|
2022-07-06 23:19:05 +02:00
|
|
|
var defaultThumbSizes = []string{"100x100"}
|
|
|
|
|
2022-10-30 10:28:14 +02:00
|
|
|
// bindFileApi registers the file api endpoints and the corresponding handlers.
|
2024-09-29 18:23:19 +02:00
|
|
|
func bindFileApi(app core.App, rg *router.RouterGroup[*core.RequestEvent]) {
|
2023-12-04 16:40:35 +02:00
|
|
|
api := fileApi{
|
2024-01-31 11:08:40 +02:00
|
|
|
thumbGenSem: semaphore.NewWeighted(int64(runtime.NumCPU() + 2)), // the value is arbitrary chosen and may change in the future
|
2023-12-04 16:40:35 +02:00
|
|
|
thumbGenPending: new(singleflight.Group),
|
|
|
|
thumbGenMaxWait: 60 * time.Second,
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
sub := rg.Group("/files")
|
|
|
|
sub.POST("/token", api.fileToken).Bind(RequireAuth())
|
|
|
|
sub.GET("/{collection}/{recordId}/{filename}", api.download).Bind(collectionPathRateLimit("", "file"))
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type fileApi struct {
|
2023-12-04 16:40:35 +02:00
|
|
|
// thumbGenSem is a semaphore to prevent too much concurrent
|
|
|
|
// requests generating new thumbs at the same time.
|
|
|
|
thumbGenSem *semaphore.Weighted
|
|
|
|
|
|
|
|
// thumbGenPending represents a group of currently pending
|
|
|
|
// thumb generation processes.
|
|
|
|
thumbGenPending *singleflight.Group
|
|
|
|
|
|
|
|
// thumbGenMaxWait is the maximum waiting time for starting a new
|
|
|
|
// thumb generation process.
|
|
|
|
thumbGenMaxWait time.Duration
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
func (api *fileApi) fileToken(e *core.RequestEvent) error {
|
|
|
|
if e.Auth == nil {
|
|
|
|
return e.UnauthorizedError("Missing auth context.", nil)
|
2023-04-04 19:33:35 +02:00
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
token, err := e.Auth.NewFileToken()
|
|
|
|
if err != nil {
|
|
|
|
return e.InternalServerError("Failed to generate file token", err)
|
|
|
|
}
|
2023-04-04 19:33:35 +02:00
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
event := new(core.FileTokenRequestEvent)
|
|
|
|
event.RequestEvent = e
|
|
|
|
event.Token = token
|
2023-07-20 09:40:03 +02:00
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
return e.App.OnFileTokenRequest().Trigger(event, func(e *core.FileTokenRequestEvent) error {
|
|
|
|
return e.JSON(http.StatusOK, map[string]string{
|
|
|
|
"token": e.Token,
|
2023-04-04 19:33:35 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
func (api *fileApi) download(e *core.RequestEvent) error {
|
|
|
|
collection, err := e.App.FindCachedCollectionByNameOrId(e.Request.PathValue("collection"))
|
|
|
|
if err != nil {
|
|
|
|
return e.NotFoundError("", nil)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
recordId := e.Request.PathValue("recordId")
|
2022-07-06 23:19:05 +02:00
|
|
|
if recordId == "" {
|
2024-09-29 18:23:19 +02:00
|
|
|
return e.NotFoundError("", nil)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
record, err := e.App.FindRecordById(collection, recordId)
|
2022-07-06 23:19:05 +02:00
|
|
|
if err != nil {
|
2024-09-29 18:23:19 +02:00
|
|
|
return e.NotFoundError("", err)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
filename := e.Request.PathValue("filename")
|
2022-07-06 23:19:05 +02:00
|
|
|
|
|
|
|
fileField := record.FindFileFieldByFile(filename)
|
|
|
|
if fileField == nil {
|
2024-09-29 18:23:19 +02:00
|
|
|
return e.NotFoundError("", nil)
|
2023-04-04 19:33:35 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 12:27:42 +02:00
|
|
|
// check whether the request is authorized to view the protected file
|
2024-09-29 18:23:19 +02:00
|
|
|
if fileField.Protected {
|
|
|
|
originalRequestInfo, err := e.RequestInfo()
|
|
|
|
if err != nil {
|
|
|
|
return e.InternalServerError("Failed to load request info", err)
|
2023-07-16 23:03:09 +02:00
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
token := e.Request.URL.Query().Get("token")
|
|
|
|
authRecord, _ := e.App.FindAuthRecordByToken(token, core.TokenTypeFile)
|
|
|
|
|
|
|
|
// create a shallow copy of the cached request data and adjust it to the current auth record (if any)
|
|
|
|
requestInfo := *originalRequestInfo
|
|
|
|
requestInfo.Context = core.RequestInfoContextProtectedFile
|
|
|
|
requestInfo.Auth = authRecord
|
|
|
|
|
|
|
|
if ok, _ := e.App.CanAccessRecord(record, &requestInfo, record.Collection().ViewRule); !ok {
|
|
|
|
return e.NotFoundError("", errors.New("insufficient permissions to access the file resource"))
|
2023-04-04 19:33:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2023-02-18 19:33:42 +02:00
|
|
|
baseFilesPath := record.BaseFilesPath()
|
|
|
|
|
|
|
|
// fetch the original view file field related record
|
|
|
|
if collection.IsView() {
|
2024-09-29 18:23:19 +02:00
|
|
|
fileRecord, err := e.App.FindRecordByViewFile(collection.Id, fileField.Name, filename)
|
2023-02-18 19:33:42 +02:00
|
|
|
if err != nil {
|
2024-09-29 18:23:19 +02:00
|
|
|
return e.NotFoundError("", fmt.Errorf("failed to fetch view file field record: %w", err))
|
2023-02-18 19:33:42 +02:00
|
|
|
}
|
|
|
|
baseFilesPath = fileRecord.BaseFilesPath()
|
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
fsys, err := e.App.NewFilesystem()
|
2022-07-06 23:19:05 +02:00
|
|
|
if err != nil {
|
2024-09-29 18:23:19 +02:00
|
|
|
return e.InternalServerError("Filesystem initialization failure.", err)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
2023-12-04 16:40:35 +02:00
|
|
|
defer fsys.Close()
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2023-02-18 19:33:42 +02:00
|
|
|
originalPath := baseFilesPath + "/" + filename
|
2022-07-06 23:19:05 +02:00
|
|
|
servedPath := originalPath
|
|
|
|
servedName := filename
|
|
|
|
|
|
|
|
// check for valid thumb size param
|
2024-09-29 18:23:19 +02:00
|
|
|
thumbSize := e.Request.URL.Query().Get("thumb")
|
|
|
|
if thumbSize != "" && (list.ExistInSlice(thumbSize, defaultThumbSizes) || list.ExistInSlice(thumbSize, fileField.Thumbs)) {
|
2022-07-06 23:19:05 +02:00
|
|
|
// extract the original file meta attributes and check it existence
|
2023-12-04 16:40:35 +02:00
|
|
|
oAttrs, oAttrsErr := fsys.Attributes(originalPath)
|
2022-07-06 23:19:05 +02:00
|
|
|
if oAttrsErr != nil {
|
2024-09-29 18:23:19 +02:00
|
|
|
return e.NotFoundError("", err)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if it is an image
|
|
|
|
if list.ExistInSlice(oAttrs.ContentType, imageContentTypes) {
|
|
|
|
// add thumb size as file suffix
|
|
|
|
servedName = thumbSize + "_" + filename
|
2023-02-18 19:33:42 +02:00
|
|
|
servedPath = baseFilesPath + "/thumbs_" + filename + "/" + servedName
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2023-12-04 16:40:35 +02:00
|
|
|
// create a new thumb if it doesn't exist
|
|
|
|
if exists, _ := fsys.Exists(servedPath); !exists {
|
2024-09-29 18:23:19 +02:00
|
|
|
if err := api.createThumb(e, fsys, originalPath, servedPath, thumbSize); err != nil {
|
|
|
|
e.App.Logger().Warn(
|
2023-12-04 16:40:35 +02:00
|
|
|
"Fallback to original - failed to create thumb "+servedName,
|
|
|
|
slog.Any("error", err),
|
|
|
|
slog.String("original", originalPath),
|
|
|
|
slog.String("thumb", servedPath),
|
|
|
|
)
|
|
|
|
|
|
|
|
// fallback to the original
|
|
|
|
servedName = filename
|
|
|
|
servedPath = originalPath
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
event := new(core.FileDownloadRequestEvent)
|
|
|
|
event.RequestEvent = e
|
2023-01-27 22:19:08 +02:00
|
|
|
event.Collection = collection
|
|
|
|
event.Record = record
|
|
|
|
event.FileField = fileField
|
|
|
|
event.ServedPath = servedPath
|
|
|
|
event.ServedName = servedName
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2023-01-30 12:54:51 +02:00
|
|
|
// clickjacking shouldn't be a concern when serving uploaded files,
|
|
|
|
// so it safe to unset the global X-Frame-Options to allow files embedding
|
|
|
|
// (note: it is out of the hook to allow users to customize the behavior)
|
2024-09-29 18:23:19 +02:00
|
|
|
e.Response.Header().Del("X-Frame-Options")
|
2023-07-20 09:40:03 +02:00
|
|
|
|
2024-09-29 18:23:19 +02:00
|
|
|
return e.App.OnFileDownloadRequest().Trigger(event, func(e *core.FileDownloadRequestEvent) error {
|
|
|
|
if err := fsys.Serve(e.Response, e.Request, e.ServedPath, e.ServedName); err != nil {
|
|
|
|
return e.NotFoundError("", err)
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2023-04-04 19:33:35 +02:00
|
|
|
|
2023-12-04 16:40:35 +02:00
|
|
|
func (api *fileApi) createThumb(
|
2024-09-29 18:23:19 +02:00
|
|
|
e *core.RequestEvent,
|
2023-12-04 16:40:35 +02:00
|
|
|
fsys *filesystem.System,
|
|
|
|
originalPath string,
|
|
|
|
thumbPath string,
|
|
|
|
thumbSize string,
|
|
|
|
) error {
|
|
|
|
ch := api.thumbGenPending.DoChan(thumbPath, func() (any, error) {
|
2024-09-29 18:23:19 +02:00
|
|
|
ctx, cancel := context.WithTimeout(e.Request.Context(), api.thumbGenMaxWait)
|
2023-12-04 16:40:35 +02:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := api.thumbGenSem.Acquire(ctx, 1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer api.thumbGenSem.Release(1)
|
|
|
|
|
|
|
|
return nil, fsys.CreateThumb(originalPath, thumbPath, thumbSize)
|
|
|
|
})
|
|
|
|
|
|
|
|
res := <-ch
|
|
|
|
|
|
|
|
api.thumbGenPending.Forget(thumbPath)
|
|
|
|
|
|
|
|
return res.Err
|
|
|
|
}
|