@cooperation/vc-storage 1.0.19 → 1.0.21
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,11 +132,73 @@ 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 file permissions
|
136
|
+
await this.fetcher({
|
137
|
+
method: 'POST',
|
138
|
+
url: `https://www.googleapis.com/drive/v3/files/${file.id}/permissions`,
|
139
|
+
headers: {},
|
140
|
+
body: JSON.stringify({
|
141
|
+
role: 'reader',
|
142
|
+
type: 'anyone',
|
143
|
+
}),
|
144
|
+
});
|
145
|
+
// Check for existing file_ids.json in appDataFolder
|
146
|
+
let existingFileId = null;
|
147
|
+
let existingFileIds = [];
|
148
|
+
try {
|
149
|
+
const existingFileQuery = await this.fetcher({
|
150
|
+
method: 'GET',
|
151
|
+
headers: {},
|
152
|
+
url: `https://www.googleapis.com/drive/v3/files?spaces=appDataFolder&q=name='file_ids.json'&fields=files(id)`,
|
153
|
+
});
|
154
|
+
if (existingFileQuery.files?.length > 0) {
|
155
|
+
existingFileId = existingFileQuery.files[0].id;
|
156
|
+
const fileContent = await this.fetcher({
|
157
|
+
method: 'GET',
|
158
|
+
headers: {},
|
159
|
+
url: `https://www.googleapis.com/drive/v3/files/${existingFileId}?alt=media`,
|
160
|
+
});
|
161
|
+
existingFileIds = JSON.parse(fileContent);
|
162
|
+
}
|
163
|
+
console.log('existingFileId', existingFileId);
|
164
|
+
}
|
165
|
+
catch (error) {
|
166
|
+
console.log('Creating new file_ids.json');
|
167
|
+
}
|
168
|
+
// Add new file ID
|
169
|
+
existingFileIds.push(file.id);
|
170
|
+
// Metadata for app data file
|
171
|
+
const appDataFileMetadata = {
|
172
|
+
name: 'file_ids.json',
|
173
|
+
mimeType: 'application/json',
|
174
|
+
spaces: ['appDataFolder'],
|
175
|
+
};
|
176
|
+
// Update or create file_ids.json
|
177
|
+
const formDataForAppData = new FormData();
|
178
|
+
formDataForAppData.append('metadata', new Blob([JSON.stringify(appDataFileMetadata)], { type: 'application/json' }));
|
179
|
+
formDataForAppData.append('file', new Blob([JSON.stringify(existingFileIds)], { type: 'application/json' }));
|
180
|
+
if (existingFileId) {
|
181
|
+
await this.fetcher({
|
182
|
+
method: 'PATCH',
|
183
|
+
headers: {},
|
184
|
+
body: formDataForAppData,
|
185
|
+
url: `https://www.googleapis.com/upload/drive/v3/files/${existingFileId}?uploadType=multipart`,
|
186
|
+
});
|
187
|
+
}
|
188
|
+
else {
|
189
|
+
await this.fetcher({
|
190
|
+
method: 'POST',
|
191
|
+
headers: {},
|
192
|
+
body: formDataForAppData,
|
193
|
+
url: `https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&spaces=appDataFolder`,
|
194
|
+
});
|
195
|
+
}
|
196
|
+
console.log(file);
|
135
197
|
return file;
|
136
198
|
}
|
137
199
|
catch (error) {
|
138
|
-
console.error('Error
|
139
|
-
|
200
|
+
console.error('Error:', error.message);
|
201
|
+
throw error;
|
140
202
|
}
|
141
203
|
}
|
142
204
|
/**
|
@@ -426,4 +488,50 @@ export class GoogleDriveStorage {
|
|
426
488
|
throw error;
|
427
489
|
}
|
428
490
|
}
|
491
|
+
async getFileIdsFromAppDataFolder() {
|
492
|
+
try {
|
493
|
+
// Step 1: Search for the file_ids.json file in the appDataFolder
|
494
|
+
const response = await this.fetcher({
|
495
|
+
method: 'GET',
|
496
|
+
headers: {},
|
497
|
+
url: `https://www.googleapis.com/drive/v3/files?q=name='file_ids.json' and 'appDataFolder' in parents&fields=files(id)`,
|
498
|
+
});
|
499
|
+
// Step 2: Check if the file exists
|
500
|
+
if (!response.files || response.files.length === 0) {
|
501
|
+
console.log('No file_ids.json found in appDataFolder.');
|
502
|
+
return [];
|
503
|
+
}
|
504
|
+
// Step 3: Get the file ID of file_ids.json
|
505
|
+
const fileId = response.files[0].id;
|
506
|
+
// Step 4: Fetch the content of file_ids.json
|
507
|
+
const fileContent = await this.fetcher({
|
508
|
+
method: 'GET',
|
509
|
+
headers: {},
|
510
|
+
url: `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`,
|
511
|
+
});
|
512
|
+
// Step 5: Parse the file content (array of file IDs)
|
513
|
+
const fileIds = JSON.parse(fileContent);
|
514
|
+
return fileIds;
|
515
|
+
}
|
516
|
+
catch (error) {
|
517
|
+
console.error('Error fetching file IDs from appDataFolder:', error.message);
|
518
|
+
return [];
|
519
|
+
}
|
520
|
+
}
|
521
|
+
async getAllFilesData() {
|
522
|
+
try {
|
523
|
+
// Step 1: Get the file IDs from appDataFolder
|
524
|
+
const fileIds = await this.getFileIdsFromAppDataFolder();
|
525
|
+
if (fileIds.length === 0) {
|
526
|
+
console.log('No files found.');
|
527
|
+
return [];
|
528
|
+
}
|
529
|
+
// Step 2: Return the array of file IDs
|
530
|
+
return fileIds;
|
531
|
+
}
|
532
|
+
catch (error) {
|
533
|
+
console.error('Error fetching all files data:', error.message);
|
534
|
+
return [];
|
535
|
+
}
|
536
|
+
}
|
429
537
|
}
|