Unverified Commit 2903f7cd authored by Niklas Hambüchen's avatar Niklas Hambüchen Committed by GitHub
Browse files

tusd: init at 1.8.0 (#167076)

parents c654f663 5fad4242
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1388,6 +1388,7 @@ in
  tuptime = handleTest ./tuptime.nix { };
  turbovnc-headless-server = handleTest ./turbovnc-headless-server.nix { };
  turn-rs = handleTest ./turn-rs.nix { };
  tusd = runTest ./tusd/default.nix;
  tuxguitar = runTest ./tuxguitar.nix;
  twingate = runTest ./twingate.nix;
  typesense = handleTest ./typesense.nix { };
+50 −0
Original line number Diff line number Diff line
{ pkgs, lib, ... }:

let
  port = 1080;

  client =
    { pkgs, ... }:
    {
      environment.systemPackages = [ pkgs.curl ];
    };

  server =
    { pkgs, ... }:
    {
      # tusd does not have a NixOS service yet.
      systemd.services.tusd = {
        wantedBy = [ "multi-user.target" ];

        serviceConfig = {
          ExecStart = ''${pkgs.tusd}/bin/tusd -port "${toString port}" -upload-dir=/data'';
        };
      };
      networking.firewall.allowedTCPPorts = [ port ];
    };
in
{
  name = "tusd";
  meta.maintainers = with lib.maintainers; [
    nh2
    kalbasit
  ];

  nodes = {
    inherit server;
    inherit client;
  };

  testScript = ''
    server.wait_for_unit("tusd.service")
    server.wait_for_open_port(${toString port})

    # Create large file.
    client.succeed("${pkgs.coreutils}/bin/truncate --size=100M file-100M.bin")

    # Upload it.
    client.succeed("${./tus-curl-upload.sh} file-100M.bin http://server:${toString port}/files/")

    print("Upload succeeded")
  '';
}
+50 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

# Adapted from:
# - https://github.com/tus/tus.io/issues/96

if [ ! -f "${1}" ]; then
  echo -e "\n\033[1;31m✘\033[0m First argument needs to be an existing file.\n"
  exit 1
fi

if [ -z "${2}" ]; then
  echo -e "\n\033[1;31m✘\033[0m Second argument needs to be the TUS server's URL.\n"
  exit 1
fi

file=${1}
TUS_URL=${2}
filename=$(basename "${file}" | base64)
filesize="$(wc -c <"${file}")"

# Apparently 'Location: ..' is terminated by CRLF. grep and awk faithfully
# preserve the line ending, and the shell's $() substitution strips off the
# final LF leaving you with a string that just ends with a CR.
#
# When the CR is printed, the cursor moves to the beginning of the line and
# whatever gets printed next overwrites what was there.
# ... | tr -d '\015'
location=$(curl \
  --silent --show-error \
  -I \
  -X POST \
  -H "Tus-Resumable: 1.0.0" \
  -H "Content-Length: 0" \
  -H "Upload-Length: ${filesize}" \
  -H "Upload-Metadata: name ${filename}" \
  "${TUS_URL}" | grep 'Location:' | awk '{print $2}' | tr -d '\015')

if [ -n "${location}" ]; then
  curl \
    -X PATCH \
    -H "Tus-Resumable: 1.0.0" \
    -H "Upload-Offset: 0" \
    -H "Content-Length: ${filesize}" \
    -H "Content-Type: application/offset+octet-stream" \
    --data-binary "@${file}" \
    "${location}" -v
else
  echo -e "\n\033[1;31m✘\033[0m File creation failed..\n"
  exit 1
fi
+38 −0
Original line number Diff line number Diff line
{
  lib,
  buildGoModule,
  fetchFromGitHub,
  nixosTests,
}:

buildGoModule rec {
  pname = "tusd";
  version = "2.8.0";

  src = fetchFromGitHub {
    owner = "tus";
    repo = "tusd";
    tag = "v${version}";
    hash = "sha256-OzXBeLDjaJk4NVgsauR/NUATh7qHbuEfWNdhytZmd0A=";
  };

  vendorHash = "sha256-YununGyB72zE0tmqO3BREJeMTjCuy/1fhPHC5r8OLjg=";

  # Tests need the path to the binary:
  # https://github.com/tus/tusd/blob/0e52ad650abed02ec961353bb0c3c8bc36650d2c/internal/e2e/e2e_test.go#L37
  preCheck = ''
    export TUSD_BINARY=$PWD/../go/bin/tusd
  '';

  passthru.tests.tusd = nixosTests.tusd;

  meta = {
    description = "Reference server implementation in Go of tus: the open protocol for resumable file uploads";
    license = lib.licenses.mit;
    homepage = "https://tus.io/";
    maintainers = with lib.maintainers; [
      nh2
      kalbasit
    ];
  };
}