@cooperation/vc-storage 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,5 +10,13 @@ import { GoogleDriveStorage } from '../models/GoogleDriveStorage.js';
10
10
  * @throws Will throw an error if the save operation fails.
11
11
  */
12
12
  export declare function saveToGoogleDrive(storage: GoogleDriveStorage, data: any, type: 'VC' | 'DID' | 'SESSION' | 'RECOMMENDATION' | 'KEYPAIR', uuid?: string): Promise<object>;
13
+ /**
14
+ * Upload an image to Google Drive in the Credentials/MEDIAs folder.
15
+ * @param {GoogleDriveStorage} storage - The GoogleDriveStorage instance.
16
+ * @param {File} imageFile - The image file to upload.
17
+ * @returns {Promise<object>} - The uploaded image file object.
18
+ * @throws Will throw an error if the upload operation fails.
19
+ */
20
+ export declare function uploadImageToGoogleDrive(storage: GoogleDriveStorage, imageFile: File): Promise<object>;
13
21
  export declare function generateViewLink(fileId: string): string;
14
22
  export declare function extractGoogleDriveFileId(url: string): string | null;
@@ -59,6 +59,63 @@ export async function saveToGoogleDrive(storage, data, type, uuid) {
59
59
  throw error;
60
60
  }
61
61
  }
62
+ /**
63
+ * Upload an image to Google Drive in the Credentials/MEDIAs folder.
64
+ * @param {GoogleDriveStorage} storage - The GoogleDriveStorage instance.
65
+ * @param {File} imageFile - The image file to upload.
66
+ * @returns {Promise<object>} - The uploaded image file object.
67
+ * @throws Will throw an error if the upload operation fails.
68
+ */
69
+ export async function uploadImageToGoogleDrive(storage, imageFile) {
70
+ try {
71
+ // Get all root folders
72
+ const rootFolders = await storage.findFolders();
73
+ console.log('Root folders:', rootFolders);
74
+ // Find or create the "Credentials" folder
75
+ let credentialsFolder = rootFolders.find((f) => f.name === 'Credentials');
76
+ let credentialsFolderId;
77
+ if (!credentialsFolder) {
78
+ credentialsFolderId = await storage.createFolder('Credentials');
79
+ console.log('Created Credentials folder with ID:', credentialsFolderId);
80
+ }
81
+ else {
82
+ credentialsFolderId = credentialsFolder.id;
83
+ console.log('Found Credentials folder with ID:', credentialsFolderId);
84
+ }
85
+ // Get subfolders within the "Credentials" folder
86
+ const subfolders = await storage.findFolders(credentialsFolderId);
87
+ console.log(`Subfolders in Credentials (ID: ${credentialsFolderId}):`, subfolders);
88
+ // Find or create the "MEDIAs" folder
89
+ let mediasFolder = subfolders.find((f) => f.name === 'MEDIAs');
90
+ let mediasFolderId;
91
+ if (!mediasFolder) {
92
+ mediasFolderId = await storage.createFolder('MEDIAs', credentialsFolderId);
93
+ console.log('Created MEDIAs folder with ID:', mediasFolderId);
94
+ }
95
+ else {
96
+ mediasFolderId = mediasFolder.id;
97
+ console.log('Found MEDIAs folder with ID:', mediasFolderId);
98
+ }
99
+ // Prepare the image file data
100
+ const imageData = {
101
+ fileName: imageFile.name,
102
+ mimeType: imageFile.type,
103
+ body: imageFile,
104
+ };
105
+ // Save the image in the "MEDIAs" folder
106
+ const uploadedImage = await storage.save(imageData, mediasFolderId);
107
+ 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
+ return uploadedImage;
113
+ }
114
+ catch (error) {
115
+ console.error('Error uploading image to Google Drive:', error);
116
+ throw error;
117
+ }
118
+ }
62
119
  export function generateViewLink(fileId) {
63
120
  if (!fileId) {
64
121
  throw new Error('File ID is required to generate a view link.');
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cooperation/vc-storage",
3
3
  "type": "module",
4
- "version": "1.0.7",
4
+ "version": "1.0.8",
5
5
  "description": "Sign and store your verifiable credentials.",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/types/index.d.ts",