@cooperation/vc-storage 1.0.8 → 1.0.10

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.
@@ -73,7 +73,7 @@ export class GoogleDriveStorage {
73
73
  const fileMetadata = {
74
74
  name: data.fileName,
75
75
  parents: [folderId], // Specify the folder ID
76
- mimeType: 'application/json', // Use provided MIME type or default to JSON
76
+ mimeType: data.mimeType,
77
77
  };
78
78
  let uploadUrl = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart';
79
79
  const formData = new FormData();
@@ -167,16 +167,66 @@ export class GoogleDriveStorage {
167
167
  * @returns file content
168
168
  */
169
169
  async retrieve(id) {
170
+ const metadataUrl = `https://www.googleapis.com/drive/v3/files/${id}?fields=name`;
171
+ const dataUrl = `https://www.googleapis.com/drive/v3/files/${id}?alt=media`;
170
172
  try {
171
- const file = await this.fetcher({
173
+ console.log(`Starting retrieval for file ID: ${id}`);
174
+ // Fetch file metadata to get the name
175
+ const metadataResponse = await fetch(metadataUrl, {
172
176
  method: 'GET',
173
- headers: {},
174
- url: `https://www.googleapis.com/drive/v3/files/${id}?alt=media`,
177
+ headers: {
178
+ Authorization: `Bearer ${this.accessToken}`,
179
+ },
175
180
  });
176
- return file;
181
+ if (!metadataResponse.ok) {
182
+ const errorData = await metadataResponse.json();
183
+ console.error(`Failed to retrieve metadata for file ID ${id}:`, errorData);
184
+ return null;
185
+ }
186
+ const metadata = await metadataResponse.json();
187
+ const fileName = metadata.name;
188
+ console.log(`File name retrieved: ${fileName}`);
189
+ // Fetch actual file data
190
+ const dataResponse = await fetch(dataUrl, {
191
+ method: 'GET',
192
+ headers: {
193
+ Authorization: `Bearer ${this.accessToken}`,
194
+ },
195
+ });
196
+ if (!dataResponse.ok) {
197
+ const errorData = await dataResponse.json();
198
+ console.error(`Failed to retrieve file data for ID ${id}:`, errorData);
199
+ return null;
200
+ }
201
+ const contentType = dataResponse.headers.get('Content-Type');
202
+ console.log(`File content type: ${contentType}`);
203
+ let fileData;
204
+ if (contentType?.includes('application/json')) {
205
+ fileData = await dataResponse.json();
206
+ }
207
+ else if (contentType?.includes('text') || contentType?.includes('image/svg+xml')) {
208
+ fileData = await dataResponse.text(); // Fetch SVG files as text for easy manipulation
209
+ }
210
+ else if (contentType?.includes('image') ||
211
+ contentType?.includes('video') ||
212
+ contentType?.includes('audio') ||
213
+ contentType?.includes('application/octet-stream') ||
214
+ contentType?.includes('application/pdf') ||
215
+ contentType?.includes('application/msword') ||
216
+ contentType?.includes('application/vnd.openxmlformats-officedocument.wordprocessingml.document') ||
217
+ contentType?.includes('application/vnd.ms-excel') ||
218
+ contentType?.includes('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')) {
219
+ fileData = await dataResponse.blob();
220
+ }
221
+ else {
222
+ fileData = await dataResponse.arrayBuffer(); // Fallback for other binary types
223
+ }
224
+ console.log('🚀 ~ GoogleDriveStorage ~ retrieve ~ fileData:', fileData);
225
+ // Return file ID, name, and data
226
+ return { id, name: fileName, data: fileData };
177
227
  }
178
228
  catch (error) {
179
- console.error('Error retrieving file:', error);
229
+ console.error(`Error retrieving file with ID ${id}:`, error.message);
180
230
  return null;
181
231
  }
182
232
  }
@@ -309,6 +359,31 @@ export class GoogleDriveStorage {
309
359
  return []; // Return an empty array on error
310
360
  }
311
361
  }
362
+ /**
363
+ * Update the name of a file in Google Drive
364
+ * @param fileId - The ID of the file to update
365
+ * @param newFileName - The new name for the file
366
+ * @returns The updated file metadata, including the new name
367
+ */
368
+ async updateFileName(fileId, newFileName) {
369
+ try {
370
+ const metadata = {
371
+ name: newFileName, // New name for the file
372
+ };
373
+ const updatedFile = await this.fetcher({
374
+ method: 'PATCH',
375
+ headers: { 'Content-Type': 'application/json' },
376
+ body: JSON.stringify(metadata),
377
+ url: `https://www.googleapis.com/drive/v3/files/${fileId}`,
378
+ });
379
+ console.log('File name updated successfully:', updatedFile.name);
380
+ return updatedFile;
381
+ }
382
+ catch (error) {
383
+ console.error('Error updating file name:', error.message);
384
+ throw error;
385
+ }
386
+ }
312
387
  /**
313
388
  * Delete file by id
314
389
  * @param id
@@ -45,7 +45,11 @@ export declare class GoogleDriveStorage {
45
45
  * @param id
46
46
  * @returns file content
47
47
  */
48
- retrieve(id: string): Promise<any>;
48
+ retrieve(id: string): Promise<{
49
+ id: string;
50
+ name: string;
51
+ data: any;
52
+ } | null>;
49
53
  /**
50
54
  * Get folder by folderId, if folderId == null you will have them all
51
55
  * @param id [Optional]
@@ -64,7 +68,14 @@ export declare class GoogleDriveStorage {
64
68
  * @param type
65
69
  * @returns
66
70
  */
67
- getAllFilesByType(type: 'KEYPAIRs' | 'VCs' | 'SESSIONs' | 'DIDs' | 'RECOMMENDATIONs'): Promise<FileContent[]>;
71
+ getAllFilesByType(type: 'KEYPAIRs' | 'VCs' | 'SESSIONs' | 'DIDs' | 'RECOMMENDATIONs' | 'MEDIAs'): Promise<FileContent[]>;
72
+ /**
73
+ * Update the name of a file in Google Drive
74
+ * @param fileId - The ID of the file to update
75
+ * @param newFileName - The new name for the file
76
+ * @returns The updated file metadata, including the new name
77
+ */
78
+ updateFileName(fileId: string, newFileName: string): Promise<any>;
68
79
  /**
69
80
  * Delete file by id
70
81
  * @param id
@@ -14,9 +14,11 @@ export declare function saveToGoogleDrive(storage: GoogleDriveStorage, data: any
14
14
  * Upload an image to Google Drive in the Credentials/MEDIAs folder.
15
15
  * @param {GoogleDriveStorage} storage - The GoogleDriveStorage instance.
16
16
  * @param {File} imageFile - The image file to upload.
17
- * @returns {Promise<object>} - The uploaded image file object.
17
+ * @returns {Promise<>} - The uploaded image file object.
18
18
  * @throws Will throw an error if the upload operation fails.
19
19
  */
20
- export declare function uploadImageToGoogleDrive(storage: GoogleDriveStorage, imageFile: File): Promise<object>;
20
+ export declare function uploadImageToGoogleDrive(storage: GoogleDriveStorage, imageFile: File): Promise<{
21
+ id: string;
22
+ }>;
21
23
  export declare function generateViewLink(fileId: string): string;
22
24
  export declare function extractGoogleDriveFileId(url: string): string | null;
@@ -63,38 +63,32 @@ export async function saveToGoogleDrive(storage, data, type, uuid) {
63
63
  * Upload an image to Google Drive in the Credentials/MEDIAs folder.
64
64
  * @param {GoogleDriveStorage} storage - The GoogleDriveStorage instance.
65
65
  * @param {File} imageFile - The image file to upload.
66
- * @returns {Promise<object>} - The uploaded image file object.
66
+ * @returns {Promise<>} - The uploaded image file object.
67
67
  * @throws Will throw an error if the upload operation fails.
68
68
  */
69
69
  export async function uploadImageToGoogleDrive(storage, imageFile) {
70
70
  try {
71
71
  // Get all root folders
72
72
  const rootFolders = await storage.findFolders();
73
- console.log('Root folders:', rootFolders);
74
73
  // Find or create the "Credentials" folder
75
74
  let credentialsFolder = rootFolders.find((f) => f.name === 'Credentials');
76
75
  let credentialsFolderId;
77
76
  if (!credentialsFolder) {
78
77
  credentialsFolderId = await storage.createFolder('Credentials');
79
- console.log('Created Credentials folder with ID:', credentialsFolderId);
80
78
  }
81
79
  else {
82
80
  credentialsFolderId = credentialsFolder.id;
83
- console.log('Found Credentials folder with ID:', credentialsFolderId);
84
81
  }
85
82
  // Get subfolders within the "Credentials" folder
86
83
  const subfolders = await storage.findFolders(credentialsFolderId);
87
- console.log(`Subfolders in Credentials (ID: ${credentialsFolderId}):`, subfolders);
88
84
  // Find or create the "MEDIAs" folder
89
85
  let mediasFolder = subfolders.find((f) => f.name === 'MEDIAs');
90
86
  let mediasFolderId;
91
87
  if (!mediasFolder) {
92
88
  mediasFolderId = await storage.createFolder('MEDIAs', credentialsFolderId);
93
- console.log('Created MEDIAs folder with ID:', mediasFolderId);
94
89
  }
95
90
  else {
96
91
  mediasFolderId = mediasFolder.id;
97
- console.log('Found MEDIAs folder with ID:', mediasFolderId);
98
92
  }
99
93
  // Prepare the image file data
100
94
  const imageData = {
@@ -105,10 +99,6 @@ export async function uploadImageToGoogleDrive(storage, imageFile) {
105
99
  // Save the image in the "MEDIAs" folder
106
100
  const uploadedImage = await storage.save(imageData, mediasFolderId);
107
101
  console.log(`Image uploaded: ${uploadedImage?.id} to MEDIAs folder in Credentials`);
108
- if (uploadedImage && uploadedImage.id) {
109
- console.log('Sharing image file with second user...');
110
- await storage.addCommenterRoleToFile(uploadedImage.id);
111
- }
112
102
  return uploadedImage;
113
103
  }
114
104
  catch (error) {
@@ -36,7 +36,7 @@ export const createAndSignVerifiablePresentation = async (accessTokens, vcFileId
36
36
  }
37
37
  }
38
38
  // Create Verifiable Presentation (VP) with the retrieved VC
39
- const presentation = await engine.createPresentation([verifiableCredential, ...recommendations]); //! do not edit the array order!!
39
+ const presentation = await engine.createPresentation([verifiableCredential.data, ...recommendations]); //! do not edit the array order!!
40
40
  // Use the key pair to sign the presentation
41
41
  const signedPresentation = await engine.signPresentation(presentation);
42
42
  return { signedPresentation };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cooperation/vc-storage",
3
3
  "type": "module",
4
- "version": "1.0.8",
4
+ "version": "1.0.10",
5
5
  "description": "Sign and store your verifiable credentials.",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/types/index.d.ts",