3a88c02326aadbb1df3436cd519f7b8a4df6ae5b
chmalee
  Thu Jun 6 15:32:15 2024 -0700
Work in progress using the mysql table for hubSpace on client side

diff --git src/hg/hgHubConnect/hooks/post-finish.c src/hg/hgHubConnect/hooks/post-finish.c
index 782d8e7..c40092e 100644
--- src/hg/hgHubConnect/hooks/post-finish.c
+++ src/hg/hgHubConnect/hooks/post-finish.c
@@ -5,30 +5,31 @@
 #include "common.h"
 #include "linefile.h"
 #include "hash.h"
 #include "options.h"
 #include "wikiLink.h"
 #include "customTrack.h"
 #include "userdata.h"
 #include "jsonQuery.h"
 #include "jsHelper.h"
 #include "errCatch.h"
 #include "obscure.h"
 #include "hooklib.h"
 #include "jksql.h"
 #include "hdb.h"
 #include "hubSpace.h"
+#include "md5.h"
 
 void usage()
 /* Explain usage and exit. */
 {
 errAbort(
   "post-finish - tus daemon post-finish hook program\n"
   "usage:\n"
   "   post-finish < input\n"
   );
 }
 
 /* Command line validation table. */
 static struct optionSpec options[] = {
    {NULL, 0},
 };
@@ -48,61 +49,65 @@
 if (!(reqId))
     {
     rejectUpload(response, "not a TUS request");
     exitStatus = 1;
     }
 else
     {
     struct errCatch *errCatch = errCatchNew(0);
     if (errCatchStart(errCatch))
         {
         // the variables for the row entry for this file, some can be NULL
         char *userName = NULL;
         char *fileName = NULL;
         long long fileSize = 0;
         char *fileType = NULL;
-        //char *hubNameList = NULL; // allocated in addHubNameToFile(), NULL is ok
         char *db = NULL;
         char *location = NULL;
+        char *reqLm = NULL;
+        time_t lastModified;
 
         struct lineFile *lf = lineFileStdin(FALSE);
         char *request = lineFileReadAll(lf);
         struct jsonElement *req = jsonParse(request);
         fprintf(stderr, "Hook request:\n");
         jsonPrintToFile(req, NULL, stderr, 0);
         char *reqCookie= jsonQueryString(req, "", "Event.HTTPRequest.Header.Cookie[0]", NULL);
         if (reqCookie)
             {
             setenv("HTTP_COOKIE", reqCookie, 0);
             }
         fprintf(stderr, "reqCookie='%s'\n", reqCookie);
         userName = (loginSystemEnabled() || wikiLinkEnabled()) ? wikiLinkUserName() : NULL;
         fprintf(stderr, "userName='%s'\n'", userName);
         if (!userName)
             {
             errAbort("not logged in");
             }
         else
             {
-            fileName = jsonQueryString(req, "", "Event.Upload.MetaData.filename", NULL);
+            // NOTE: All Upload.MetaData values are strings
+            fileName = jsonQueryString(req, "", "Event.Upload.MetaData.fileName", 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);
+            lastModified = sqlLongLong(reqLm) / 1000; // yes Javascript dates are in millis
             char *tusFile = jsonQueryString(req, "", "Event.Upload.Storage.Path", NULL);
             if (fileName == NULL)
                 {
-                errAbort("No Event.Upload.filename setting");
+                errAbort("No Event.Upload.fileName setting");
                 }
             else if (tusFile == NULL)
                 {
                 errAbort("No Event.Path setting");
                 }
             else
                 {
                 char *tusInfo = catTwoStrings(tusFile, ".info");
                 char *dataDir = getDataDir(userName);
                 char *newFile = catTwoStrings(dataDir, fileName);
                 fprintf(stderr, "moving %s to %s\n", tusFile, newFile);
                 // TODO: check if file exists or not and let user choose to overwrite
                 // and re-call this hook, for now just exit if the file exists
                 if (fileExists(newFile))
                     {
@@ -130,44 +135,48 @@
                     mustRemove(tusInfo);
                     }
                 }
             }
 
         // we've passed all the checks so we can write a new or updated row
         // to the mysql table and return to the client that we were successful
         if (exitStatus == 0)
             {
             struct hubSpace *row = NULL;
             AllocVar(row);
             row->userName = userName;
             row->fileName = fileName;
             row->fileSize = fileSize;
             row->fileType = fileType;
-            row->creationTime = NULL;
-            row->lastModified = NULL;
+            row->creationTime = NULL; // automatically handled by mysql
+            row->lastModified = sqlUnixTimeToDate(&lastModified, TRUE);
             row->hubNameList = NULL;
             row->db = db;
             row->location = location;
+            row->md5sum = md5HexForFile(row->location);
             struct sqlConnection *conn = hConnectCentral();
 
             // now write out row to hubSpace table
             if (!sqlTableExistsOnMain(conn, "hubSpace"))
                 {
                 errAbort("No hubSpace MySQL table is present. Please send us an email");
                 }
             struct dyString *sqlUpdateStmt = dyStringNew(0);
-            sqlDyStringPrintf(sqlUpdateStmt, "insert into hubSpace values ('%s', '%s', %llu, '%s', '%s', '', '', '', '%s')", row->userName, row->fileName, row->fileSize, row->fileType, row->db, row->location);
+            sqlDyStringPrintf(sqlUpdateStmt, "insert into hubSpace values ('%s', '%s', %llu, "
+                    "'%s', NULL, '%s', '', '%s', '%s', '%s')",
+                    row->userName, row->fileName, row->fileSize, row->fileType,
+                    row->lastModified, row->db, row->location, row->md5sum);
             fprintf(stderr, "%s\n", sqlUpdateStmt->string);
             fflush(stderr);
             sqlUpdate(conn, sqlUpdateStmt->string);
             }
         }
     if (errCatch->gotError)
         {
         rejectUpload(response, errCatch->message->string);
         exitStatus = 1;
         }
     errCatchEnd(errCatch);
     }
 // always print a response no matter what
 jsonPrintToFile(response, NULL, stdout, 0);
 return exitStatus;