Fetch remote files when not in indexeddb

This commit is contained in:
Magnus Åhall 2026-06-26 14:07:26 +02:00
parent f3ca7b9a44
commit 6df2be7944
6 changed files with 153 additions and 36 deletions

42
main.go
View file

@ -148,6 +148,9 @@ func main() { // {{{
http.HandleFunc("/node/history/retrieve/{uuid}/{offset}", authenticated(actionNodeHistoryRetrieve))
http.HandleFunc("/node/history/count/{uuid}", authenticated(actionNodeHistoryCount))
http.HandleFunc("/file/{uuid}", authenticated(actionFile))
http.HandleFunc("/file/{uuid}/metadata", authenticated(actionFileMetadata))
http.HandleFunc("/service_worker.js", pageServiceWorker)
listen := fmt.Sprintf("%s:%d", config.Network.Address, config.Network.Port)
@ -168,6 +171,11 @@ func authenticated(fn func(http.ResponseWriter, *http.Request)) func(http.Respon
// The Bearer token is extracted.
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
authHeader = "Bearer " + r.URL.Query().Get("jwt")
}
authParts := RxpBearerToken.FindStringSubmatch(authHeader)
if len(authParts) != 2 {
failed(fmt.Errorf("Authorization missing or invalid"))
@ -404,9 +412,9 @@ func actionSyncFileUpload(w http.ResponseWriter, r *http.Request) { // {{{
file := File{}
if sentBytes == 0 {
file.UUID = uuid
file.Filename = r.Header.Get("X-File-Name")
file.Name = r.Header.Get("X-File-Name")
file.Size, _ = strconv.Atoi(r.Header.Get("X-File-Size"))
file.MIME = r.Header.Get("X-File-Type")
file.Type = r.Header.Get("X-File-Type")
modded, _ := strconv.ParseInt(r.Header.Get("X-File-Modified"), 10, 64)
file.Modified = time.UnixMilli(modded)
err := AddFileToDb(session.UserID, &file)
@ -492,6 +500,36 @@ func actionUserSetPreferences(w http.ResponseWriter, r *http.Request) { // {{{
})
} // }}}
func actionFile(w http.ResponseWriter, r *http.Request) {// {{{
uuid := r.PathValue("uuid")
Log.Info("FOO", "uuid", uuid)
session := getUserSession(r)
f, md, err := FileForUser(session.UserID, uuid)
if err != nil {
Log.Error("file_metadata", "error", err)
httpError(w, err)
return
}
http.ServeContent(w, r, md.Name, md.Modified, f)
}// }}}
func actionFileMetadata(w http.ResponseWriter, r *http.Request) {// {{{
uuid := r.PathValue("uuid")
session := getUserSession(r)
md, err := FileMetadata(session.UserID, uuid)
if err != nil {
Log.Error("file_metadata", "error", err)
httpError(w, err)
return
}
responseData(w, map[string]any{
"OK": true,
"Metadata": md,
})
}// }}}
func createNewUser(username string) { // {{{
reader := bufio.NewReader(os.Stdin)