From 6344eaa17d461cae717a1bde1bafd7b5df3acd61 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Thu, 6 Jul 2023 00:10:07 -0400 Subject: [PATCH] Handle Old Google Drive links that used 10-char shareID When the Homebrewery was first made, editIds and ShareIds only had 10 characters. We later increased this to 12. However this means some old, old Google Drive links (in the form of `googleId + editId`) were being split incorrectly because they assumed the newer 12-char length, accidentally cutting the last 2 chars from the googleId. --- server/app.js | 4 ++-- server/googleActions.js | 8 ++++---- server/homebrew.api.js | 9 +++++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/server/app.js b/server/app.js index 901349d97..3db1897dd 100644 --- a/server/app.js +++ b/server/app.js @@ -324,8 +324,8 @@ app.get('/share/:id', asyncHandler(getBrew('share')), asyncHandler(async (req, r }; if(req.params.id.length > 12 && !brew._id) { - const googleId = req.params.id.slice(0, -12); - const shareId = req.params.id.slice(-12); + const googleId = brew.googleId; + const shareId = brew.shareId; await GoogleActions.increaseView(googleId, shareId, 'share', brew) .catch((err)=>{next(err);}); } else { diff --git a/server/googleActions.js b/server/googleActions.js index 49de3c077..898164db1 100644 --- a/server/googleActions.js +++ b/server/googleActions.js @@ -100,12 +100,12 @@ const GoogleActions = { const drive = googleDrive.drive({ version: 'v3', auth }); const fileList = []; - let NextPageToken = ""; + let NextPageToken = ''; do { const obj = await drive.files.list({ pageSize : 1000, - pageToken : NextPageToken || "", + pageToken : NextPageToken || '', fields : 'nextPageToken, files(id, name, description, createdTime, modifiedTime, properties)', q : 'mimeType != \'application/vnd.google-apps.folder\' and trashed = false' }) @@ -243,9 +243,9 @@ const GoogleActions = { if(obj) { if(accessType == 'edit' && obj.data.properties.editId != accessId){ - throw ('Edit ID does not match'); + throw ({ message: 'Edit ID does not match' }); } else if(accessType == 'share' && obj.data.properties.shareId != accessId){ - throw ('Share ID does not match'); + throw ({ message: 'Share ID does not match' }); } const file = await drive.files.get({ diff --git a/server/homebrew.api.js b/server/homebrew.api.js index 60c86b6d4..52f5cf88f 100644 --- a/server/homebrew.api.js +++ b/server/homebrew.api.js @@ -27,8 +27,13 @@ const api = { // If the id is longer than 12, then it's a google id + the edit id. This splits the longer id up. if(id.length > 12) { - googleId = id.slice(0, -12); - id = id.slice(-12); + if(id.length >= (33 + 12)) { // googleId is minimum 33 chars (may increase) + googleId = id.slice(0, -12); // current editId is 12 chars + } else { // old editIds used to be 10 chars; + googleId = id.slice(0, -10); // if total string is too short, must be old brew + console.log('Old brew, using 10-char Id'); + } + id = id.slice(googleId.length); } return { id, googleId }; },