c6d419ecfb6f122d7a0cb99c37f56c32c089bea3
chmalee
  Thu Jan 22 10:41:38 2026 -0800
HubSpace changes: Allow users to overwrite already uploaded files, but warn them first. When a hub.txt file is being uploaded, don't auto-generate one. Check if the tracks being uploaded already exist in the hub.txt before adding a default stanza for it. Add some more checks in the hook files to prevent faulty uploads. Have the tus client clear localStorage on successful uploads so re-uploads of the same file don't cause confusing errors

diff --git src/hg/hgHubConnect/hooks/pre-finish.c src/hg/hgHubConnect/hooks/pre-finish.c
index df27659ea25..8bb05e0fff0 100644
--- src/hg/hgHubConnect/hooks/pre-finish.c
+++ src/hg/hgHubConnect/hooks/pre-finish.c
@@ -79,52 +79,59 @@
         char *reqCookie= jsonQueryString(req, "", "Event.HTTPRequest.Header.Cookie[0]", NULL);
         if (reqCookie)
             {
             setenv("HTTP_COOKIE", reqCookie, 0);
             }
         fprintf(stderr, "reqCookie='%s'\n", reqCookie);
         userName = getUserName();
         if (!userName)
             {
             // maybe an apiKey was provided, use that instead to look up the userName
             char *apiKey = jsonQueryString(req, "", "Event.Upload.MetaData.apiKey", NULL);
             userName = userNameForApiKey(NULL, apiKey);
             if (!userName)
                 errAbort("You are not logged in. Please navigate to My Data -> My Sessions and log in or create an account.");
             }
-        fprintf(stderr, "userName='%s'\n'", userName);
+        fprintf(stderr, "userName='%s'\n", userName);
         // NOTE: All Upload.MetaData values are strings
-        fileName = cgiEncodeFull(jsonQueryString(req, "", "Event.Upload.MetaData.fileName", NULL));
+        // Check multiple possible metadata keys for filename (Uppy sends 'filename' and 'name' by default,
+        // our JS code also sets 'fileName' - try all to handle resumed uploads with old metadata)
+        char *rawFileName = jsonQueryString(req, "", "Event.Upload.MetaData.fileName", NULL);
+        if (!rawFileName)
+            rawFileName = jsonQueryString(req, "", "Event.Upload.MetaData.filename", NULL);
+        if (!rawFileName)
+            rawFileName = jsonQueryString(req, "", "Event.Upload.MetaData.name", NULL);
+        fileName = rawFileName ? cgiEncodeFull(rawFileName) : NULL;
         fileSize = jsonQueryInt(req, "",  "Event.Upload.Size", 0, NULL);
         fileType = jsonQueryString(req, "", "Event.Upload.MetaData.fileType", NULL);
         db = jsonQueryString(req, "", "Event.Upload.MetaData.genome", NULL);
         reqLm = jsonQueryString(req, "", "Event.Upload.MetaData.lastModified", NULL);
+        if (reqLm)
             lastModified = sqlLongLong(reqLm) / 1000; // yes Javascript dates are in millis
+        else
+            lastModified = time(NULL); // fallback to current time if not provided
         parentDir = jsonQueryString(req, "", "Event.Upload.MetaData.parentDir", NULL);
-        fprintf(stderr, "parentDir = '%s'\n", parentDir);
-        fflush(stderr);
+        fprintf(stderr, "parentDir = '%s'\n", parentDir ? parentDir : "(null)");
         // strip out plain leading '.' and '/' components
         // middle '.' components are dealt with later
-        if (startsWith("./", parentDir) || startsWith("/", parentDir))
+        if (parentDir && (startsWith("./", parentDir) || startsWith("/", parentDir)))
             parentDir = skipBeyondDelimit(parentDir, '/');
-        fprintf(stderr, "parentDir = '%s'\n", parentDir);
-        fflush(stderr);
         tusFile = jsonQueryString(req, "", "Event.Upload.Storage.Path", NULL);
         tusInfo = jsonQueryString(req, "", "Event.Upload.Storage.InfoPath", NULL);
         if (fileName == NULL)
             {
-            errAbort("No Event.Upload.fileName setting");
+            errAbort("No filename found in upload metadata (checked fileName, filename, and name)");
             }
         else if (tusFile == NULL)
             {
             errAbort("No Event.Path setting");
             }
         else
             {
             userDataDir = dataDir = getDataDir(userName);
             // if parentDir provided we are throwing the files in there
             if (parentDir)
                 {
                 encodedParentDir = encodePath(parentDir);
                 if (!endsWith(encodedParentDir, "/"))
                     encodedParentDir = catTwoStrings(encodedParentDir, "/");
                 dataDir = catTwoStrings(dataDir, encodedParentDir);
@@ -169,31 +176,31 @@
                 {
                 // all upload data should have been received and thus the realpath
                 // should not fail, but just in case, put something valid here
                 row->location = tusFile;
                 }
             row->md5sum = md5HexForFile(row->location);
             row->parentDir = encodedParentDir ? encodedParentDir : "";
             if (!isHubToolsUpload && !(sameString(fileType, "hub.txt")))
                 {
                 createNewTempHubForUpload(reqId, row, userDataDir, encodedParentDir);
                 fprintf(stderr, "added hub.txt and hubSpace row for hub for file: '%s'\n", fileName);
                 fflush(stderr);
                 }
             // first make the parentDir rows
             makeParentDirRows(row->userName, sqlDateToUnixTime(row->lastModified), row->db, row->parentDir, userDataDir);
-            row->parentDir = hubNameFromPath(encodedParentDir);
+            row->parentDir = encodedParentDir ? hubNameFromPath(encodedParentDir) : "";
             addHubSpaceRowForFile(row);
             fprintf(stderr, "added hubSpace row for file '%s'\n", fileName);
             fflush(stderr);
             }
         }
     if (errCatch->gotError)
         {
         rejectUpload(response, errCatch->message->string);
         // must remove the tusd temp files so if the users tries again after a temp error
         // the upload will work
         if (tusFile)
             {
             mustRemove(tusFile);
             mustRemove(tusInfo);
             }