-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloudStorageService.java
More file actions
251 lines (198 loc) · 10.7 KB
/
Copy pathCloudStorageService.java
File metadata and controls
251 lines (198 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.securefileshare.services;
import java.io.*;
import com.google.api.services.drive.Drive;
public class CloudStorageService {
private static CloudStorageService instance;
private GoogleDriveService googleDriveService;
private CloudStorageService() {
System.out.println("=== INITIALIZING CLOUD STORAGE SERVICE ===");
try {
googleDriveService = GoogleDriveService.getInstance();
if (googleDriveService.isServiceAvailable()) {
System.out.println("✓ Using Google Drive as cloud storage backend");
} else {
System.err.println("✗ Google Drive service is not available");
}
} catch (Exception e) {
System.err.println("✗ CLOUD STORAGE INITIALIZATION WARNING!");
System.err.println("Error: " + e.getMessage());
}
}
public static synchronized CloudStorageService getInstance() {
if (instance == null) {
instance = new CloudStorageService();
}
return instance;
}
public boolean isServiceAvailable() {
return googleDriveService != null && googleDriveService.isServiceAvailable();
}
public String testConnection() {
StringBuilder result = new StringBuilder();
result.append("=== CLOUD STORAGE CONNECTION TEST ===\n\n");
if (googleDriveService == null) {
result.append("✗ Google Drive service not initialized\n");
} else if (googleDriveService.isServiceAvailable()) {
result.append("✓ Google Drive: AVAILABLE\n");
result.append("\nService Details:\n");
result.append("- Storage Provider: Google Drive\n");
result.append("- Status: Operational\n");
result.append("- Authentication: OAuth 2.0\n");
result.append("- Maximum File Size: 5TB\n");
result.append("- Free Storage: 15GB\n");
} else {
result.append("✗ Google Drive: UNAVAILABLE\n");
result.append("\nTroubleshooting:\n");
result.append("1. Check internet connection\n");
result.append("2. Verify credentials.json exists in src/main/resources/\n");
result.append("3. Ensure Google Drive API is enabled in Google Cloud Console\n");
result.append("4. Check OAuth consent screen configuration\n");
}
return result.toString();
}
private Drive getDriveService() throws Exception {
if (googleDriveService == null) {
throw new Exception("Google Drive service not initialized");
}
return googleDriveService.getDriveService();
}
public UploadResult uploadFileWithEncryption(byte[] fileData, String filename, String folder,
int userId, boolean encrypt, String encryptionPassword,
EncryptionService.EncryptionResult encryptionResult) {
System.out.println("\n" + "=".repeat(70));
System.out.println("=== CLOUD STORAGE UPLOAD WITH ENCRYPTION ===");
System.out.println("User ID: " + userId);
System.out.println("Filename: " + filename);
System.out.println("Encryption: " + (encrypt ? "ENABLED" : "DISABLED"));
System.out.println("File Size: " + fileData.length + " bytes");
System.out.println("Google Drive Available: " + (googleDriveService != null && googleDriveService.isServiceAvailable()));
System.out.println("=".repeat(70));
UploadResult result = new UploadResult();
try {
if (googleDriveService == null || !googleDriveService.isServiceAvailable()) {
throw new IOException("Google Drive service is not available");
}
byte[] dataToUpload = fileData;
if (encrypt && encryptionResult != null) {
dataToUpload = encryptionResult.getEncryptedData();
System.out.println("✓ File encrypted");
System.out.println("Original size: " + fileData.length + " bytes");
System.out.println("Encrypted size: " + dataToUpload.length + " bytes");
}
GoogleDriveService.UploadResult gdriveResult;
if (encrypt && encryptionResult != null) {
gdriveResult = googleDriveService.uploadFile(
dataToUpload, filename, userId,
"Encrypted: " + encrypt + " | Password protected",
encrypt,
encryptionResult.getEncryptionKey(),
encryptionResult.getIv()
);
} else {
gdriveResult = googleDriveService.uploadFile(
dataToUpload, filename, userId,
"Uploaded via Secure File Sharing System",
encrypt
);
}
result.setSuccess(gdriveResult.isSuccess());
result.setCloudFileId(gdriveResult.getCloudFileId());
result.setWebdavPath(gdriveResult.getWebdavPath());
result.setOriginalSize(fileData.length);
result.setFilename(gdriveResult.getFilename());
result.setOriginalFilename(filename);
result.setShareableUrl(gdriveResult.getShareableUrl());
result.setEncrypted(encrypt);
if (encrypt && encryptionResult != null) {
result.setEncryptionInfo("key:" + encryptionResult.getEncryptionKey() + ":iv:" + encryptionResult.getIv());
}
if (!gdriveResult.isSuccess()) {
result.setErrorMessage(gdriveResult.getErrorMessage());
}
System.out.println("✓ Upload successful!");
System.out.println("File ID: " + gdriveResult.getCloudFileId());
System.out.println("Shareable URL: " + gdriveResult.getShareableUrl());
} catch (Exception e) {
System.err.println("✗ UPLOAD FAILED!");
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
result.setSuccess(false);
result.setErrorMessage("Google Drive upload failed: " + e.getMessage());
}
return result;
}
public byte[] downloadFile(String fileId) throws Exception {
System.out.println("\n=== DOWNLOADING FROM GOOGLE DRIVE ===");
System.out.println("File ID: " + fileId);
if (!isServiceAvailable()) {
throw new Exception("Google Drive service is not available");
}
try {
Drive driveService = getDriveService();
Drive.Files.Get getRequest = driveService.files().get(fileId);
getRequest.setFields("id, name, size, mimeType");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
getRequest.executeMediaAndDownloadTo(outputStream);
byte[] fileData = outputStream.toByteArray();
System.out.println("✓ Download successful!");
System.out.println("File size: " + fileData.length + " bytes");
System.out.println("File ID: " + fileId);
return fileData;
}
} catch (Exception e) {
System.err.println("✗ Download failed: " + e.getMessage());
e.printStackTrace();
throw new Exception("Failed to download file from Google Drive: " + e.getMessage(), e);
}
}
public boolean deleteFile(String fileId) {
System.out.println("\n=== DELETING FROM GOOGLE DRIVE ===");
System.out.println("File ID: " + fileId);
if (!isServiceAvailable()) {
System.err.println("Google Drive service is not available");
return false;
}
try {
Drive driveService = getDriveService();
driveService.files().delete(fileId).execute();
System.out.println("✓ File deleted successfully from Google Drive");
return true;
} catch (Exception e) {
System.err.println("✗ Failed to delete from Google Drive: " + e.getMessage());
e.printStackTrace();
return false;
}
}
public static class UploadResult {
private boolean success;
private String cloudFileId;
private String webdavPath;
private String filename;
private String originalFilename;
private long originalSize;
private boolean encrypted;
private String encryptionInfo;
private String shareableUrl;
private String errorMessage;
public boolean isSuccess() { return success; }
public void setSuccess(boolean success) { this.success = success; }
public String getCloudFileId() { return cloudFileId; }
public void setCloudFileId(String cloudFileId) { this.cloudFileId = cloudFileId; }
public String getWebdavPath() { return webdavPath; }
public void setWebdavPath(String webdavPath) { this.webdavPath = webdavPath; }
public String getFilename() { return filename; }
public void setFilename(String filename) { this.filename = filename; }
public String getOriginalFilename() { return originalFilename; }
public void setOriginalFilename(String originalFilename) { this.originalFilename = originalFilename; }
public long getOriginalSize() { return originalSize; }
public void setOriginalSize(long originalSize) { this.originalSize = originalSize; }
public boolean isEncrypted() { return encrypted; }
public void setEncrypted(boolean encrypted) { this.encrypted = encrypted; }
public String getEncryptionInfo() { return encryptionInfo; }
public void setEncryptionInfo(String encryptionInfo) { this.encryptionInfo = encryptionInfo; }
public String getShareableUrl() { return shareableUrl; }
public void setShareableUrl(String shareableUrl) { this.shareableUrl = shareableUrl; }
public String getErrorMessage() { return errorMessage; }
public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }
}
}