@cooperation/vc-storage 1.0.18 → 1.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -132,10 +132,65 @@ export class GoogleDriveStorage {
|
|
132
132
|
body: formData,
|
133
133
|
url: `https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,parents`,
|
134
134
|
});
|
135
|
+
// Set the file permission to "Anyone with the link" can view
|
136
|
+
const permissionUrl = `https://www.googleapis.com/drive/v3/files/${file.id}/permissions`;
|
137
|
+
const permissionData = {
|
138
|
+
role: 'reader',
|
139
|
+
type: 'anyone', // Public access
|
140
|
+
};
|
141
|
+
await this.fetcher({
|
142
|
+
method: 'POST',
|
143
|
+
url: permissionUrl,
|
144
|
+
headers: {},
|
145
|
+
body: JSON.stringify(permissionData),
|
146
|
+
});
|
147
|
+
// Step 9: Save the file ID in the appDataFolder
|
148
|
+
console.log('Saving file ID to appDataFolder...');
|
149
|
+
const appDataFileMetadata = {
|
150
|
+
name: 'file_ids.json', // File to store file IDs
|
151
|
+
parents: ['appDataFolder'], // Save in the hidden appDataFolder
|
152
|
+
mimeType: 'application/json',
|
153
|
+
};
|
154
|
+
// Step 10: Check if an existing file_ids.json exists in appDataFolder
|
155
|
+
let existingFileIds = [];
|
156
|
+
try {
|
157
|
+
const existingFile = await this.fetcher({
|
158
|
+
method: 'GET',
|
159
|
+
headers: {},
|
160
|
+
url: `https://www.googleapis.com/drive/v3/files?q=name='file_ids.json' and 'appDataFolder' in parents&fields=files(id)`,
|
161
|
+
});
|
162
|
+
if (existingFile.files && existingFile.files.length > 0) {
|
163
|
+
const fileId = existingFile.files[0].id;
|
164
|
+
const fileContent = await this.fetcher({
|
165
|
+
method: 'GET',
|
166
|
+
headers: {},
|
167
|
+
url: `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`,
|
168
|
+
});
|
169
|
+
existingFileIds = JSON.parse(fileContent);
|
170
|
+
}
|
171
|
+
}
|
172
|
+
catch (error) {
|
173
|
+
console.log('No existing file_ids.json found, creating a new one.');
|
174
|
+
}
|
175
|
+
// Step 11: Append the new file ID to the existing list
|
176
|
+
existingFileIds.push(file.id);
|
177
|
+
// Step 12: Create a Blob for the updated file IDs
|
178
|
+
const appDataFileBlob = new Blob([JSON.stringify(existingFileIds)], { type: 'application/json' });
|
179
|
+
// Step 13: Upload the updated file_ids.json to appDataFolder
|
180
|
+
const appDataFormData = new FormData();
|
181
|
+
appDataFormData.append('metadata', new Blob([JSON.stringify(appDataFileMetadata)], { type: 'application/json' }));
|
182
|
+
appDataFormData.append('file', appDataFileBlob);
|
183
|
+
await this.fetcher({
|
184
|
+
method: 'POST',
|
185
|
+
headers: {},
|
186
|
+
body: appDataFormData,
|
187
|
+
url: `https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id`,
|
188
|
+
});
|
189
|
+
console.log('File ID saved to appDataFolder.');
|
135
190
|
return file;
|
136
191
|
}
|
137
192
|
catch (error) {
|
138
|
-
console.error('Error uploading file or
|
193
|
+
console.error('Error uploading file or saving file ID:', error.message);
|
139
194
|
return null;
|
140
195
|
}
|
141
196
|
}
|
@@ -426,4 +481,58 @@ export class GoogleDriveStorage {
|
|
426
481
|
throw error;
|
427
482
|
}
|
428
483
|
}
|
484
|
+
async getFileIdsFromAppDataFolder() {
|
485
|
+
try {
|
486
|
+
// Step 1: Search for the file_ids.json file in the appDataFolder
|
487
|
+
const response = await this.fetcher({
|
488
|
+
method: 'GET',
|
489
|
+
headers: {},
|
490
|
+
url: `https://www.googleapis.com/drive/v3/files?q=name='file_ids.json' and 'appDataFolder' in parents&fields=files(id)`,
|
491
|
+
});
|
492
|
+
// Step 2: Check if the file exists
|
493
|
+
if (!response.files || response.files.length === 0) {
|
494
|
+
console.log('No file_ids.json found in appDataFolder.');
|
495
|
+
return [];
|
496
|
+
}
|
497
|
+
// Step 3: Get the file ID of file_ids.json
|
498
|
+
const fileId = response.files[0].id;
|
499
|
+
// Step 4: Fetch the content of file_ids.json
|
500
|
+
const fileContent = await this.fetcher({
|
501
|
+
method: 'GET',
|
502
|
+
headers: {},
|
503
|
+
url: `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`,
|
504
|
+
});
|
505
|
+
// Step 5: Parse the file content (array of file IDs)
|
506
|
+
const fileIds = JSON.parse(fileContent);
|
507
|
+
return fileIds;
|
508
|
+
}
|
509
|
+
catch (error) {
|
510
|
+
console.error('Error fetching file IDs from appDataFolder:', error.message);
|
511
|
+
return [];
|
512
|
+
}
|
513
|
+
}
|
514
|
+
async getAllFilesData() {
|
515
|
+
try {
|
516
|
+
// Step 1: Get the file IDs from appDataFolder
|
517
|
+
const fileIds = await this.getFileIdsFromAppDataFolder();
|
518
|
+
if (fileIds.length === 0) {
|
519
|
+
console.log('No files found.');
|
520
|
+
return [];
|
521
|
+
}
|
522
|
+
// Step 2: Fetch data for each file ID
|
523
|
+
const filesData = [];
|
524
|
+
for (const fileId of fileIds) {
|
525
|
+
const fileData = await this.retrieve(fileId);
|
526
|
+
if (fileData) {
|
527
|
+
filesData.push(fileData);
|
528
|
+
}
|
529
|
+
}
|
530
|
+
// Step 3: Return the array of file data
|
531
|
+
return filesData;
|
532
|
+
}
|
533
|
+
catch (error) {
|
534
|
+
console.error('Error fetching all files data:', error.message);
|
535
|
+
return [];
|
536
|
+
}
|
537
|
+
}
|
429
538
|
}
|