From 2fc09a2a1e91a41d565ec9657a17344e45d4159f Mon Sep 17 00:00:00 2001 From: Joe Corall Date: Tue, 23 Jul 2024 17:01:01 -0400 Subject: [PATCH] [minor] Add basic http handler to check work (#2) --- go.mod | 19 +- go.sum | 86 +++++++++ google/appsscript/Multi-Dropdown.gs | 29 +++ google/appsscript/appsscript.json | 12 ++ google/appsscript/check.gs | 55 ++++++ internal/handlers/check.go | 264 ++++++++++++++++++++++++++++ main.go | 19 +- 7 files changed, 482 insertions(+), 2 deletions(-) create mode 100644 google/appsscript/Multi-Dropdown.gs create mode 100644 google/appsscript/appsscript.json create mode 100644 google/appsscript/check.gs create mode 100644 internal/handlers/check.go diff --git a/go.mod b/go.mod index 949692f..674e5ad 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,21 @@ module github.com/lehigh-university-libraries/fabricator go 1.22.2 -require github.com/lehigh-university-libraries/go-islandora v0.0.0-20240709193244-50f8e60d633d +require ( + github.com/golang-jwt/jwt/v5 v5.2.1 + github.com/lehigh-university-libraries/go-islandora v0.0.0-20240709193244-50f8e60d633d + github.com/lestrrat-go/jwx v1.2.29 + github.com/sfomuseum/go-edtf v1.1.1 +) + +require ( + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect + github.com/lestrrat-go/blackmagic v1.0.2 // indirect + github.com/lestrrat-go/httpcc v1.0.1 // indirect + github.com/lestrrat-go/iter v1.0.2 // indirect + github.com/lestrrat-go/option v1.0.1 // indirect + github.com/pkg/errors v0.9.1 // indirect + golang.org/x/crypto v0.21.0 // indirect +) diff --git a/go.sum b/go.sum index a87f394..d49f58c 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,88 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk= +github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/lehigh-university-libraries/go-islandora v0.0.0-20240709193244-50f8e60d633d h1:bryeojGZvWZazzCdVsZB0Pi3LijX/Aq82KgDnjPkt0s= github.com/lehigh-university-libraries/go-islandora v0.0.0-20240709193244-50f8e60d633d/go.mod h1:JDCARba/UJW608jcs6XyVuCsfp3LoDVDC++bnGAB47A= +github.com/lestrrat-go/backoff/v2 v2.0.8 h1:oNb5E5isby2kiro9AgdHLv5N5tint1AnDVVf2E2un5A= +github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y= +github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k= +github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU= +github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE= +github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E= +github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI= +github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4= +github.com/lestrrat-go/jwx v1.2.29 h1:QT0utmUJ4/12rmsVQrJ3u55bycPkKqGYuGT4tyRhxSQ= +github.com/lestrrat-go/jwx v1.2.29/go.mod h1:hU8k2l6WF0ncx20uQdOmik/Gjg6E3/wIRtXSNFeZuB8= +github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I= +github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU= +github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sfomuseum/go-edtf v1.1.1 h1:R5gElndHGDaK/rGSh2X+ulaLtlcHCdQA1cTzB8e9wv8= +github.com/sfomuseum/go-edtf v1.1.1/go.mod h1:1rP0EJZ/84j3HO80vGcnG2T9MFBDAFyTNtjrr8cv3T4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/google/appsscript/Multi-Dropdown.gs b/google/appsscript/Multi-Dropdown.gs new file mode 100644 index 0000000..80f2746 --- /dev/null +++ b/google/appsscript/Multi-Dropdown.gs @@ -0,0 +1,29 @@ +function onEdit(e) { + var ss = SpreadsheetApp.getActiveSpreadsheet(); + var sheet = ss.getActiveSheet(); + if (sheet.getName() != "Sheet1") { + return; + } + + var activeCell = ss.getActiveCell(); + var columnNames = ["Related Department", "Language"]; + var columnName = sheet.getRange(1, activeCell.getColumn()).getValue(); + var pattern = /^Contributor Relator \d+$/; + if (!columnNames.includes(columnName) && !pattern.test(columnName)) { + return; + } + + var delimiter = ' ; '; + var newValue = e.value; + var oldValue = e.oldValue; + if (!newValue) { + activeCell.setValue(""); + } + else { + if (!oldValue) { + activeCell.setValue(newValue); + } else { + activeCell.setValue(oldValue + delimiter + newValue); + } + } +} diff --git a/google/appsscript/appsscript.json b/google/appsscript/appsscript.json new file mode 100644 index 0000000..0d378e6 --- /dev/null +++ b/google/appsscript/appsscript.json @@ -0,0 +1,12 @@ +{ + "timeZone": "America/New_York", + "dependencies": {}, + "exceptionLogging": "STACKDRIVER", + "runtimeVersion": "V8", + "oauthScopes": [ + "https://www.googleapis.com/auth/userinfo.email", + "https://www.googleapis.com/auth/spreadsheets.currentonly", + "https://www.googleapis.com/auth/script.external_request", + "https://www.googleapis.com/auth/script.container.ui" + ] +} diff --git a/google/appsscript/check.gs b/google/appsscript/check.gs new file mode 100644 index 0000000..2b3c2d4 --- /dev/null +++ b/google/appsscript/check.gs @@ -0,0 +1,55 @@ +function onOpen() { + var ui = SpreadsheetApp.getUi(); + ui.createMenu('Lehigh Preserve') + .addItem('Check My Work', 'sendSheetData') + .addToUi(); +} + +function sendSheetData() { + var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); + var data = sheet.getDataRange().getValues(); + for (var i = 0; i < data.length; i++) { + for (var j = 0; j < data[i].length; j++) { + data[i][j] = data[i][j].toString(); + } + } + var payload = JSON.stringify(data); + var url = 'https://preserve.lehigh.edu/workbench/check'; + const oauthToken = ScriptApp.getIdentityToken(); + var options = { + method: 'POST', + headers: { + 'Authorization': 'Bearer ' + oauthToken + }, + contentType: 'application/json', + payload: payload + }; + + var lastRow = sheet.getLastRow(); + var lastColumn = sheet.getLastColumn(); + var range = sheet.getRange(2, 1, lastRow - 1, lastColumn); // A2 to last cell + range.setBackground(null); + range.clearNote(); + + var response = UrlFetchApp.fetch(url, options); + var t = response.getContentText() + if (t.length == 2) { + SpreadsheetApp.getUi().alert('Looks good! 🚀'); + return; + } + var result = JSON.parse(t); + displayErrors(result); +} + +function displayErrors(e) { + var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); + + var count = 0; + for (var cell in e) { + var error = e[cell]; + sheet.getRange(cell).setBackground('red').setNote(error); + count += 1; + } + + SpreadsheetApp.getUi().alert('Found ' + count + ' errors highlighted in the sheet.'); +} diff --git a/internal/handlers/check.go b/internal/handlers/check.go new file mode 100644 index 0000000..c76be11 --- /dev/null +++ b/internal/handlers/check.go @@ -0,0 +1,264 @@ +package handlers + +import ( + "context" + "encoding/json" + "fmt" + "log/slog" + "net/http" + "net/url" + "os" + "regexp" + "strconv" + "strings" + + "github.com/golang-jwt/jwt/v5" + "github.com/lestrrat-go/jwx/jwk" + edtf "github.com/sfomuseum/go-edtf/parser" +) + +const googleCertsURL = "https://www.googleapis.com/oauth2/v3/certs" + +func CheckMyWork(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + authHeader := r.Header.Get("Authorization") + if authHeader == "" { + slog.Error("Authorization header missing") + http.Error(w, "Authorization header missing", http.StatusUnauthorized) + return + } + + parts := strings.Split(authHeader, " ") + if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" { + slog.Error("Token not found") + http.Error(w, "Invalid Authorization header format", http.StatusUnauthorized) + return + } + + tokenString := parts[1] + token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { + if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok { + return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) + } + + kid, ok := token.Header["kid"].(string) + if !ok { + return nil, fmt.Errorf("kid not found in token header") + } + + ctx := context.Background() + jwksSet, err := jwk.Fetch(ctx, googleCertsURL) + if err != nil { + return nil, fmt.Errorf("unable to fetch JWK set from %s: %v", googleCertsURL, err) + } + key, ok := jwksSet.LookupKeyID(kid) + if !ok { + return nil, fmt.Errorf("unable to find key '%s'", kid) + } + + var pubkey interface{} + if err := key.Raw(&pubkey); err != nil { + return nil, fmt.Errorf("failed to get raw key: %v", err) + } + + return pubkey, nil + }) + + if err != nil || !token.Valid { + slog.Error("Unable to validate token", "err", err) + http.Error(w, "Invalid token", http.StatusUnauthorized) + return + } + + claims := token.Claims.(jwt.MapClaims) + emailVerified, ok := claims["email_verified"].(bool) + if !emailVerified || !ok { + slog.Error("Unverified email", "err", err) + http.Error(w, "Invalid token", http.StatusUnauthorized) + return + } + + email, ok := claims["email"].(string) + if !ok || len(email) < 11 || email[len(email)-11:] != "@lehigh.edu" { + http.Error(w, "Error extracting email from token", http.StatusInternalServerError) + return + } + + if r.ContentLength == 0 { + http.Error(w, "Request body is empty", http.StatusBadRequest) + return + } + + defer r.Body.Close() + + var csvData [][]string + err = json.NewDecoder(r.Body).Decode(&csvData) + if err != nil { + slog.Error("Error parsing CSV", "err", err) + http.Error(w, "Error parsing CSV", http.StatusBadRequest) + return + } + + if len(csvData) < 2 { + http.Error(w, "No rows in CSV to process", http.StatusBadRequest) + } + + header := csvData[0] + doiPattern := regexp.MustCompile(`^10\.\d{4,9}\/[-._;()/:A-Za-z0-9]+$`) + gettyTgnPattern := regexp.MustCompile(`^http://vocab\.getty\.edu/page/tgn/\d+$`) + datePattern := regexp.MustCompile(`^\d{4}(-\d{2}(-\d{2})?)?$`) + + errors := map[string]string{} + requiredFields := []string{ + "Title", + "Object Model", + } + uploadIds := map[string]bool{} + for rowIndex, row := range csvData[1:] { + for colIndex, col := range row { + column := header[colIndex] + c := numberToExcelColumn(colIndex) + i := c + strconv.Itoa(rowIndex+2) + if col == "" { + if strInSlice(column, requiredFields) { + errors[i] = "Missing value" + } + + continue + } + + for _, cell := range strings.Split(col, " ; ") { + cell = strings.TrimSpace(cell) + if cell == "" { + continue + } + + switch column { + // make sure these columns are integers + case "Parent Collection", "PPI": + _, err := strconv.Atoi(cell) + if err != nil { + errors[i] = "Must be an integer" + } + // make sure these columns are valid URLs + case "Catalog or ArchivesSpace URL": + parsedURL, err := url.ParseRequestURI(cell) + if err != nil || parsedURL.Scheme == "" && parsedURL.Host == "" { + errors[i] = "Invalid URL" + } + // make sure each upload ID is unique + case "Upload ID": + if _, exists := uploadIds[cell]; exists { + errors[i] = "Duplicate upload ID" + } + uploadIds[cell] = true + // check for valid EDTF values + case "Creation Date", "Date Captured", "Embargo Until Date": + if !datePattern.MatchString(cell) && !edtf.IsValid(cell) { + errors[i] = "Invalid EDTF value" + slog.Error("Invalid EDTF value", "cell", cell) + } + // check for valid DOI value + case "DOI": + if !doiPattern.MatchString(cell) { + errors[i] = "Invalid DOI" + } + // make sure the parent ID matches an upload ID in the spreadsheet + case "Page/Item Parent ID": + if _, ok := uploadIds[cell]; !ok { + errors[i] = "Unknown parent ID" + } + // make sure the file exists in the filesystem + case "File Path": + filename := strings.ReplaceAll(cell, `\`, `/`) + filename = strings.TrimLeft(filename, "/") + if len(filename) > 3 && filename[0:3] != "mnt" { + filename = fmt.Sprintf("/mnt/islandora_staging/%s", filename) + } + + filename = strings.ReplaceAll(filename, "/mnt/islandora_staging", "/data") + if !fileExists(filename) { + errors[i] = "File does not exist in islandora_staging" + } + case "Add Coverpage (Y/N)", "Make Public (Y/N)": + if cell != "Yes" && cell != "No" { + errors[i] = "Invalid value. Must be Yes or No" + } + case "Hierarchical Geographic (Getty TGN)": + if !gettyTgnPattern.MatchString(cell) { + errors[i] = "Invalid Getty TGN URI" + } + hierarchyURL := strings.Replace(cell, "page", "hierarchy", 1) + + req, err := http.NewRequest("GET", hierarchyURL, nil) + if err != nil { + break + } + req.Header.Set("Accept", "application/json") + + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + slog.Error("Unable to request hierarchy URL", "url", hierarchyURL, "err", err) + errors[i] = "Unable to request hierarchical information" + break + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + slog.Error("Unable to get hierarchy URL", "url", hierarchyURL, "err", err) + errors[i] = "Unable to get hierarchical information" + } + } + } + } + } + + w.Header().Set("Content-Type", "application/json") + jsonResponse, err := json.Marshal(errors) + if err != nil { + slog.Error("Error creating JSON response", "err", err) + http.Error(w, "Error creating JSON response", http.StatusInternalServerError) + return + } + + _, err = w.Write(jsonResponse) + if err != nil { + slog.Error("Error writing JSON response", "err", err) + http.Error(w, "Error writing JSON response", http.StatusInternalServerError) + } +} + +func strInSlice(s string, sl []string) bool { + for _, a := range sl { + if a == s { + return true + } + } + return false +} + +func numberToExcelColumn(n int) string { + result := "" + for { + char := 'A' + rune(n%26) + result = string(char) + result + n = n/26 - 1 + if n < 0 { + break + } + } + return result +} + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/main.go b/main.go index afe1c61..74408cb 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,12 @@ import ( "flag" "fmt" "log/slog" + "net/http" "os" "reflect" "strings" + "github.com/lehigh-university-libraries/fabricator/internal/handlers" "github.com/lehigh-university-libraries/go-islandora/workbench" ) @@ -95,7 +97,7 @@ func readCSVWithJSONTags(filePath string) ([]map[string][]string, error) { str = strings.ReplaceAll(str, `\`, `/`) str = strings.TrimLeft(str, "/") if len(str) > 3 && str[0:3] != "mnt" { - str = fmt.Sprintf("/mnt/scans/%s", str) + str = fmt.Sprintf("/mnt/islandora_staging/%s", str) } } @@ -115,6 +117,21 @@ func readCSVWithJSONTags(filePath string) ([]map[string][]string, error) { } func main() { + var serverMode bool + + flag.BoolVar(&serverMode, "server", false, "Set to true to run as server") + flag.Parse() + + if serverMode { + // Start HTTP server + http.HandleFunc("/workbench/check", handlers.CheckMyWork) + + slog.Info("Starting server on :8080") + if err := http.ListenAndServe(":8080", nil); err != nil { + panic(err) + } + } + // Define the source and target flags source := flag.String("source", "", "Path to the source CSV file") target := flag.String("target", "", "Path to the target CSV file")