Skip to content

Commit

Permalink
Maybe working?
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgodbolt committed Jan 1, 2025
1 parent fb4f89c commit 317b43b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
34 changes: 31 additions & 3 deletions src/google-drive.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const CLIENT_ID = "356883185894-bhim19837nroivv18p0j25gecora60r5.apps.googleuser
const SCOPES = "https://www.googleapis.com/auth/drive.file";
const DISCOVERY_DOC = "https://www.googleapis.com/discovery/v1/apis/drive/v3/rest";
const FILE_FIELDS = "id,name,capabilities";
const PARENT_FOLDER_NAME = "jsbeeb disc images";

const boundary = "-------314159265358979323846";
const delimiter = `\r
Expand All @@ -22,6 +23,7 @@ export class GoogleDriveLoader {
constructor() {
this.gapi = null;
this.authorized = false;
this.parentFolderId = undefined;
}

async initialise() {
Expand Down Expand Up @@ -77,7 +79,6 @@ export class GoogleDriveLoader {
this.authorized = true;
resolve(true);
};
window.moo = this.tokenClient; // DO NOT CHECK IN
this.tokenClient.error_callback = (resp) => {
console.log(`Token client failure: ${resp.type}; failed to authorize`);
reject(new Error(`Token client failure: ${resp.type}; failed to authorize`));
Expand All @@ -100,13 +101,40 @@ export class GoogleDriveLoader {
return result;
}

saveFile(name, data, idOrNone) {
async _findOrCreateParentFolder() {
// Not sure why this doesn't find Matt's pre-existing "jsbeeb disc images" folder, but this will have to do.
const list = await this.gapi.client.drive.files.list({
q: `name = '${PARENT_FOLDER_NAME}' and mimeType = 'application/vnd.google-apps.folder' and trashed = false`,
corpora: "user",
});
if (list.result.files.length === 1) {
console.log("Found existing parent folder");
return list.result.files[0].id;
}
console.log(`Creating parent folder ${PARENT_FOLDER_NAME}`);
const file = await this.gapi.client.drive.files.create({
resource: {
name: PARENT_FOLDER_NAME,
mimeType: "application/vnd.google-apps.folder",
},
fields: "id",
});
console.log("Folder Id:", file.result.id);
return file.result.id;
}

async saveFile(name, data, idOrNone) {
if (this.parentFolderId === undefined) {
this.parentFolderId = await this._findOrCreateParentFolder();
}
const metadata = {
name,
// parents: ["jsbeeb disc images"], // TODO: parents doesn't work, need folder ID maybe?
parents: [this.parentFolderId],
mimeType: MIME_TYPE,
};

// Note to self we can't upload data using create()
// https://stackoverflow.com/questions/34905363/create-file-with-google-drive-api-v3-javascript
const str = utils.uint8ArrayToString(data);
const base64Data = btoa(str);
const multipartRequestBody = `${delimiter}Content-Type: application/json\r
Expand Down
3 changes: 2 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,8 @@ $("#google-drive form").on("submit", function (e) {
loadingFinished();
},
function (error) {
loadingFinished(JSON.stringify(error));
console.log(`Error in creating: ${error} | ${JSON.stringify(error)}`);
loadingFinished(`Create failed: ${error}`);
},
);
});
Expand Down

0 comments on commit 317b43b

Please sign in to comment.