@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 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
- };
135
+ // Set file permissions
141
136
  await this.fetcher({
142
137
  method: 'POST',
143
- url: permissionUrl,
138
+ url: `https://www.googleapis.com/drive/v3/files/${file.id}/permissions`,
144
139
  headers: {},
145
- body: JSON.stringify(permissionData),
140
+ body: JSON.stringify({
141
+ role: 'reader',
142
+ type: 'anyone',
143
+ }),
146
144
  });
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
145
+ // Check for existing file_ids.json in appDataFolder
146
+ let existingFileId = null;
155
147
  let existingFileIds = [];
156
148
  try {
157
- const existingFile = await this.fetcher({
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' and 'appDataFolder' in parents&fields=files(id)`,
152
+ url: `https://www.googleapis.com/drive/v3/files?spaces=appDataFolder&q=name='file_ids.json'&fields=files(id)`,
161
153
  });
162
- if (existingFile.files && existingFile.files.length > 0) {
163
- const fileId = existingFile.files[0].id;
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/${fileId}?alt=media`,
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('No existing file_ids.json found, creating a new one.');
166
+ console.log('Creating new file_ids.json');
174
167
  }
175
- // Step 11: Append the new file ID to the existing list
168
+ // Add new file ID
176
169
  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.');
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 uploading file or saving file ID:', error.message);
194
- return null;
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: 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;
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 {};
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cooperation/vc-storage",
3
3
  "type": "module",
4
- "version": "1.0.20",
4
+ "version": "1.0.21",
5
5
  "description": "Sign and store your verifiable credentials.",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/types/index.d.ts",