Skip to content

Commit

Permalink
upload/append with file-offset
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Dec 29, 2024
1 parent 9a376ac commit 3993a54
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
16 changes: 8 additions & 8 deletions minetest.conf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
mapserver.url = http://mtui_mapserver:8080
server_announce = false
beerchat.matterbridge_token = my-token
beerchat.matterbridge_url = http://mtui_matterbridge:4242
profiler.default_report_format = json
max_users = 13
profiler.load = true
name = mtui_dev
secure.http_mods = mtui,beerchat,mapserver
mtui.key = mykey
server_name = mtui-xxx
server_announce = false
secure.http_mods = mtui,beerchat,mapserver
mapserver.url = http://mtui_mapserver:8080
max_users = 13
mtui.url = http://ui:8080
beerchat.matterbridge_url = http://mtui_matterbridge:4242
beerchat.matterbridge_token = my-token
profiler.load = true
mtui.key = mykey
2 changes: 1 addition & 1 deletion public/js/api/filebrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function postProgress(url, body, callback) {
});
}

export const append = (filename, data) => fetch(`api/filebrowser/file?filename=${filename}`, {
export const append = (filename, data, offset) => fetch(`api/filebrowser/file?filename=${filename}&offset=${offset}`, {
method: "PUT",
body: data
});
Expand Down
2 changes: 1 addition & 1 deletion public/js/service/uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function upload_chunked(dir, filename, data, progress_callback) {
let offset = 0;
do {
const chunksize = Math.min(data.size - offset, 1000*1000*2); // 2 mb chunks
await append(dir + "/" + tmpfilename, data.slice(offset, offset + chunksize));
await append(dir + "/" + tmpfilename, data.slice(offset, offset + chunksize), offset);
offset += chunksize;

if (typeof(progress_callback) == "function") {
Expand Down
8 changes: 2 additions & 6 deletions web/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,10 @@ func (api *Api) Secure(fn SecureHandlerFunc) http.HandlerFunc {
func (api *Api) OptionalSecure(fn SecureHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
claims, err := api.GetClaims(r)
if err == err_unauthorized {
// not logged in
if err != nil {
// not logged in or auth error
fn(w, r, nil)
return
} else if err != nil {
api.RemoveClaims(w)
SendError(w, http.StatusInternalServerError, err)
return
}
// logged in
fn(w, r, claims)
Expand Down
23 changes: 23 additions & 0 deletions web/filebrowser_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"mtui/types"
"net/http"
"os"
"strconv"
)

func (a *Api) UploadZip(w http.ResponseWriter, r *http.Request, claims *types.Claims) {
Expand Down Expand Up @@ -56,6 +57,28 @@ func (a *Api) AppendFile(w http.ResponseWriter, r *http.Request, claims *types.C
}

f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)

offset_str := r.URL.Query().Get("offset")
if offset_str != "" {
offset, err := strconv.ParseInt(offset_str, 10, 64)
if err != nil {
SendError(w, 500, fmt.Errorf("parse offset error '%s': %v", offset_str, err))
return
}

err = f.Truncate(offset)
if err != nil {
SendError(w, 500, fmt.Errorf("truncate error, offset=%d: %v", offset, err))
return
}

_, err = f.Seek(offset, 0)
if err != nil {
SendError(w, 500, fmt.Errorf("seek error, offset=%d: %v", offset, err))
return
}
}

if err != nil {
SendError(w, 500, fmt.Errorf("openfile error for '%s': %v", filename, err))
return
Expand Down

0 comments on commit 3993a54

Please sign in to comment.