From 2a9d2a1738028c40204eab493840b5dad01ebdb0 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 5 Dec 2020 12:37:42 +0100 Subject: tv elm-package-proxy: allow replacing own packages --- tv/2configs/elm-packages-proxy.nix | 45 +++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) (limited to 'tv') diff --git a/tv/2configs/elm-packages-proxy.nix b/tv/2configs/elm-packages-proxy.nix index 9493a40b0..097d706c6 100644 --- a/tv/2configs/elm-packages-proxy.nix +++ b/tv/2configs/elm-packages-proxy.nix @@ -66,6 +66,7 @@ in { port = cfg.port; script = /* sh */ ''. ${pkgs.writeDash "elm-packages-proxy.sh" '' PATH=${lib.makeBinPath [ + pkgs.attr pkgs.coreutils pkgs.curl pkgs.findutils @@ -151,20 +152,44 @@ in { user=$req_x_user version=$req_x_version + action=uploading + force=''${req_x_force-false} zipball=${cfg.packageDir}/$author/$pname/$version/zipball + elmjson=$HOME/cache/$author%2F$pname%2F$version%2Felm.json + endpointjson=$HOME/cache/$author%2F$pname%2F$version%2Fendpoint.json if test -e "$zipball"; then - string_response 409 Conflict \ - "package already exists: $author/$pname@$version" \ - text/plain - else - echo "user $user is uploading package $author/$pname@$version" >&2 - mkdir -p "$(dirname "$zipball")" - head -c $req_content_length > "$zipball" - string_response 200 OK \ - "package created: $author/$pname@$version" \ - text/plain + if test "$force" = true; then + zipball_owner=$(attr -q -g X-User "$zipball" || :) + if test "$zipball_owner" = "$req_x_user"; then + action=replacing + rm -f "$elmjson" + rm -f "$endpointjson" + else + string_response 403 Forbidden \ + "package already exists: $author/$pname@$version" \ + text/plain + exit + fi + else + string_response 409 Conflict \ + "package already exists: $author/$pname@$version" \ + text/plain + exit + fi fi + + echo "user $user is $action package $author/$pname@$version" >&2 + # TODO check package + mkdir -p "$(dirname "$zipball")" + head -c $req_content_length > "$zipball" + + attr -q -s X-User -V "$user" "$zipball" || : + + string_response 200 OK \ + "package created: $author/$pname@$version" \ + text/plain + exit ;; 'GET /all-packages'|'POST /all-packages') -- cgit v1.2.3