@cooperation/vc-storage 1.0.20 → 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,66 +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
|
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
|
-
};
|
135
|
+
// Set file permissions
|
141
136
|
await this.fetcher({
|
142
137
|
method: 'POST',
|
143
|
-
url:
|
138
|
+
url: `https://www.googleapis.com/drive/v3/files/${file.id}/permissions`,
|
144
139
|
headers: {},
|
145
|
-
body: JSON.stringify(
|
140
|
+
body: JSON.stringify({
|
141
|
+
role: 'reader',
|
142
|
+
type: 'anyone',
|
143
|
+
}),
|
146
144
|
});
|
147
|
-
//
|
148
|
-
|
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
|
145
|
+
// Check for existing file_ids.json in appDataFolder
|
146
|
+
let existingFileId = null;
|
155
147
|
let existingFileIds = [];
|
156
148
|
try {
|
157
|
-
const
|
149
|
+
const existingFileQuery = await this.fetcher({
|
158
150
|
method: 'GET',
|
159
151
|
headers: {},
|
160
|
-
url: `https://www.googleapis.com/drive/v3/files?q=name='file_ids.json'
|
152
|
+
url: `https://www.googleapis.com/drive/v3/files?spaces=appDataFolder&q=name='file_ids.json'&fields=files(id)`,
|
161
153
|
});
|
162
|
-
if (
|
163
|
-
|
154
|
+
if (existingFileQuery.files?.length > 0) {
|
155
|
+
existingFileId = existingFileQuery.files[0].id;
|
164
156
|
const fileContent = await this.fetcher({
|
165
157
|
method: 'GET',
|
166
158
|
headers: {},
|
167
|
-
url: `https://www.googleapis.com/drive/v3/files/${
|
159
|
+
url: `https://www.googleapis.com/drive/v3/files/${existingFileId}?alt=media`,
|
168
160
|
});
|
169
161
|
existingFileIds = JSON.parse(fileContent);
|
170
162
|
}
|
163
|
+
console.log('existingFileId', existingFileId);
|
171
164
|
}
|
172
165
|
catch (error) {
|
173
|
-
console.log('
|
166
|
+
console.log('Creating new file_ids.json');
|
174
167
|
}
|
175
|
-
//
|
168
|
+
// Add new file ID
|
176
169
|
existingFileIds.push(file.id);
|
177
|
-
//
|
178
|
-
const
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
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);
|
190
197
|
return file;
|
191
198
|
}
|
192
199
|
catch (error) {
|
193
|
-
console.error('Error
|
194
|
-
|
200
|
+
console.error('Error:', error.message);
|
201
|
+
throw error;
|
195
202
|
}
|
196
203
|
}
|
197
204
|
/**
|
@@ -519,16 +526,8 @@ export class GoogleDriveStorage {
|
|
519
526
|
console.log('No files found.');
|
520
527
|
return [];
|
521
528
|
}
|
522
|
-
// Step 2:
|
523
|
-
|
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;
|
529
|
+
// Step 2: Return the array of file IDs
|
530
|
+
return fileIds;
|
532
531
|
}
|
533
532
|
catch (error) {
|
534
533
|
console.error('Error fetching all files data:', error.message);
|
@@ -85,6 +85,6 @@ export declare class GoogleDriveStorage {
|
|
85
85
|
delete(id: string): Promise<any>;
|
86
86
|
update(fileId: string, data: any): Promise<any>;
|
87
87
|
getFileIdsFromAppDataFolder(): Promise<any>;
|
88
|
-
getAllFilesData(): Promise<any
|
88
|
+
getAllFilesData(): Promise<any>;
|
89
89
|
}
|
90
90
|
export {};
|