| 1 | // A chaotic file upload and management system |
| 2 | // Difficulty: 3 - Moderate refactoring needed |
| 3 | |
| 4 | // Types |
| 5 | type FileData = { |
| 6 | id: string; |
| 7 | name: string; |
| 8 | size: number; |
| 9 | type: string; |
| 10 | uploadDate: Date; |
| 11 | uploadedBy: string; |
| 12 | path: string; |
| 13 | isPublic: boolean; |
| 14 | tags?: string[]; |
| 15 | metadata?: Record<string, any>; |
| 16 | }; |
| 17 | |
| 18 | type User = { |
| 19 | id: string; |
| 20 | name: string; |
| 21 | email: string; |
| 22 | role: "admin" | "user" | "guest"; |
| 23 | }; |
| 24 | |
| 25 | // Global storage |
| 26 | const fileStorage: Record<string, FileData> = {}; |
| 27 | const users: Record<string, User> = { |
| 28 | user1: { |
| 29 | id: "user1", |
| 30 | name: "Admin User", |
| 31 | email: "admin@example.com", |
| 32 | role: "admin", |
| 33 | }, |
| 34 | user2: { |
| 35 | id: "user2", |
| 36 | name: "Regular User", |
| 37 | email: "user@example.com", |
| 38 | role: "user", |
| 39 | }, |
| 40 | user3: { |
| 41 | id: "user3", |
| 42 | name: "Guest User", |
| 43 | email: "guest@example.com", |
| 44 | role: "guest", |
| 45 | }, |
| 46 | }; |
| 47 | let currentUser: string | null = null; |
| 48 | let storageUsed = 0; |
| 49 | const MAX_STORAGE = 1024 * 1024 * 1024; // 1GB |
| 50 | const MAX_FILE_SIZE = 100 * 1024 * 1024; // 100MB |
| 51 | |
| 52 | // Login function |
| 53 | function login(userId: string) { |
| 54 | if (users[userId]) { |
| 55 | currentUser = userId; |
| 56 | console.log(`Logged in as ${users[userId].name}`); |
| 57 | return true; |
| 58 | } else { |
| 59 | console.error("User not found"); |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Logout function |
| 65 | function logout() { |
| 66 | currentUser = null; |
| 67 | console.log("Logged out"); |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | // Upload file |
| 72 | function uploadFile( |
| 73 | fileName: string, |
| 74 | fileSize: number, |
| 75 | fileType: string, |
| 76 | isPublic: boolean = false, |
| 77 | tags: string[] = [], |
| 78 | metadata: Record<string, any> = {}, |
| 79 | ) { |
| 80 | // Check if user is logged in |
| 81 | if (!currentUser) { |
| 82 | console.error("User not logged in"); |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | // Check file size |
| 87 | if (fileSize > MAX_FILE_SIZE) { |
| 88 | console.error( |
| 89 | `File too large. Maximum size is ${MAX_FILE_SIZE / (1024 * 1024)}MB`, |
| 90 | ); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // Check available storage |
| 95 | if (storageUsed + fileSize > MAX_STORAGE) { |
| 96 | console.error("Not enough storage space"); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // Generate ID |
| 101 | const fileId = "file_" + Date.now() + "_" + Math.floor(Math.random() * 1000); |
| 102 | |
| 103 | // Create file path |
| 104 | const path = `/storage/${currentUser}/${fileId}_${fileName}`; |
| 105 | |
| 106 | // Create file data |
| 107 | const fileData: FileData = { |
| 108 | id: fileId, |
| 109 | name: fileName, |
| 110 | size: fileSize, |
| 111 | type: fileType, |
| 112 | uploadDate: new Date(), |
| 113 | uploadedBy: currentUser, |
| 114 | path: path, |
| 115 | isPublic: isPublic, |
| 116 | tags: tags, |
| 117 | metadata: metadata, |
| 118 | }; |
| 119 | |
| 120 | // Save file data |
| 121 | fileStorage[fileId] = fileData; |
| 122 | |
| 123 | // Update storage used |
| 124 | storageUsed += fileSize; |
| 125 | |
| 126 | console.log(`File ${fileName} uploaded successfully`); |
| 127 | return fileId; |
| 128 | } |
| 129 | |
| 130 | // Delete file |
| 131 | function deleteFile(fileId: string) { |
| 132 | // Check if user is logged in |
| 133 | if (!currentUser) { |
| 134 | console.error("User not logged in"); |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | // Check if file exists |
| 139 | if (!fileStorage[fileId]) { |
| 140 | console.error("File not found"); |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | // Check if user has permission |
| 145 | const file = fileStorage[fileId]; |
| 146 | const user = users[currentUser]; |
| 147 | if (file.uploadedBy !== currentUser && user.role !== "admin") { |
| 148 | console.error("Permission denied"); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | // Update storage used |
| 153 | storageUsed -= file.size; |
| 154 | |
| 155 | // Remove file |
| 156 | delete fileStorage[fileId]; |
| 157 | |
| 158 | console.log(`File ${file.name} deleted successfully`); |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | // Update file metadata |
| 163 | function updateFileMetadata( |
| 164 | fileId: string, |
| 165 | updates: { |
| 166 | name?: string; |
| 167 | isPublic?: boolean; |
| 168 | tags?: string[]; |
| 169 | metadata?: Record<string, any>; |
| 170 | }, |
| 171 | ) { |
| 172 | // Check if user is logged in |
| 173 | if (!currentUser) { |
| 174 | console.error("User not logged in"); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | // Check if file exists |
| 179 | if (!fileStorage[fileId]) { |
| 180 | console.error("File not found"); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // Check if user has permission |
| 185 | const file = fileStorage[fileId]; |
| 186 | const user = users[currentUser]; |
| 187 | if (file.uploadedBy !== currentUser && user.role !== "admin") { |
| 188 | console.error("Permission denied"); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | // Update file data |
| 193 | if (updates.name !== undefined) fileStorage[fileId].name = updates.name; |
| 194 | if (updates.isPublic !== undefined) |
| 195 | fileStorage[fileId].isPublic = updates.isPublic; |
| 196 | if (updates.tags !== undefined) fileStorage[fileId].tags = updates.tags; |
| 197 | if (updates.metadata !== undefined) { |
| 198 | fileStorage[fileId].metadata = { |
| 199 | ...fileStorage[fileId].metadata, |
| 200 | ...updates.metadata, |
| 201 | }; |
| 202 | } |
| 203 | |
| 204 | console.log(`File ${file.name} updated successfully`); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | // Get file data |
| 209 | function getFileData(fileId: string) { |
| 210 | // Check if file exists |
| 211 | if (!fileStorage[fileId]) { |
| 212 | console.error("File not found"); |
| 213 | return null; |
| 214 | } |
| 215 | |
| 216 | const file = fileStorage[fileId]; |
| 217 | |
| 218 | // Check if user has permission |
| 219 | if ( |
| 220 | !file.isPublic && |
| 221 | currentUser !== file.uploadedBy && |
| 222 | users[currentUser]?.role !== "admin" |
| 223 | ) { |
| 224 | console.error("Permission denied"); |
| 225 | return null; |
| 226 | } |
| 227 | |
| 228 | return file; |
| 229 | } |
| 230 | |
| 231 | // Search files |
| 232 | function searchFiles(query: { |
| 233 | name?: string; |
| 234 | type?: string; |
| 235 | tags?: string[]; |
| 236 | uploadedBy?: string; |
| 237 | isPublic?: boolean; |
| 238 | minSize?: number; |
| 239 | maxSize?: number; |
| 240 | uploadedAfter?: Date; |
| 241 | uploadedBefore?: Date; |
| 242 | }) { |
| 243 | // Check if user is logged in |
| 244 | if (!currentUser) { |
| 245 | console.error("User not logged in"); |
| 246 | return []; |
| 247 | } |
| 248 | |
| 249 | const user = users[currentUser]; |
| 250 | const results: FileData[] = []; |
| 251 | |
| 252 | // Filter files |
| 253 | for (const fileId in fileStorage) { |
| 254 | const file = fileStorage[fileId]; |
| 255 | |
| 256 | // Skip files user doesn't have access to |
| 257 | if ( |
| 258 | !file.isPublic && |
| 259 | file.uploadedBy !== currentUser && |
| 260 | user.role !== "admin" |
| 261 | ) { |
| 262 | continue; |
| 263 | } |
| 264 | |
| 265 | // Apply filters |
| 266 | let match = true; |
| 267 | |
| 268 | if ( |
| 269 | query.name && |
| 270 | !file.name.toLowerCase().includes(query.name.toLowerCase()) |
| 271 | ) { |
| 272 | match = false; |
| 273 | } |
| 274 | |
| 275 | if (query.type && file.type !== query.type) { |
| 276 | match = false; |
| 277 | } |
| 278 | |
| 279 | if (query.uploadedBy && file.uploadedBy !== query.uploadedBy) { |
| 280 | match = false; |
| 281 | } |
| 282 | |
| 283 | if (query.isPublic !== undefined && file.isPublic !== query.isPublic) { |
| 284 | match = false; |
| 285 | } |
| 286 | |
| 287 | if (query.minSize !== undefined && file.size < query.minSize) { |
| 288 | match = false; |
| 289 | } |
| 290 | |
| 291 | if (query.maxSize !== undefined && file.size > query.maxSize) { |
| 292 | match = false; |
| 293 | } |
| 294 | |
| 295 | if (query.uploadedAfter && file.uploadDate < query.uploadedAfter) { |
| 296 | match = false; |
| 297 | } |
| 298 | |
| 299 | if (query.uploadedBefore && file.uploadDate > query.uploadedBefore) { |
| 300 | match = false; |
| 301 | } |
| 302 | |
| 303 | if (query.tags && query.tags.length > 0) { |
| 304 | if (!file.tags) { |
| 305 | match = false; |
| 306 | } else { |
| 307 | for (const tag of query.tags) { |
| 308 | if (!file.tags.includes(tag)) { |
| 309 | match = false; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | if (match) { |
| 317 | results.push(file); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return results; |
| 322 | } |
| 323 | |
| 324 | // Get storage stats |
| 325 | function getStorageStats() { |
| 326 | return { |
| 327 | used: storageUsed, |
| 328 | total: MAX_STORAGE, |
| 329 | available: MAX_STORAGE - storageUsed, |
| 330 | fileCount: Object.keys(fileStorage).length, |
| 331 | }; |
| 332 | } |
| 333 | |
| 334 | // Share file with user |
| 335 | function shareFile(fileId: string, targetUserId: string) { |
| 336 | // Check if user is logged in |
| 337 | if (!currentUser) { |
| 338 | console.error("User not logged in"); |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | // Check if file exists |
| 343 | if (!fileStorage[fileId]) { |
| 344 | console.error("File not found"); |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | // Check if target user exists |
| 349 | if (!users[targetUserId]) { |
| 350 | console.error("Target user not found"); |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | // Check if user has permission |
| 355 | const file = fileStorage[fileId]; |
| 356 | if (file.uploadedBy !== currentUser && users[currentUser]?.role !== "admin") { |
| 357 | console.error("Permission denied"); |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | // Share file by adding metadata |
| 362 | if (!file.metadata) { |
| 363 | file.metadata = {}; |
| 364 | } |
| 365 | |
| 366 | if (!file.metadata.sharedWith) { |
| 367 | file.metadata.sharedWith = []; |
| 368 | } |
| 369 | |
| 370 | // Check if already shared |
| 371 | if (!file.metadata.sharedWith.includes(targetUserId)) { |
| 372 | file.metadata.sharedWith.push(targetUserId); |
| 373 | } |
| 374 | |
| 375 | console.log(`File ${file.name} shared with ${users[targetUserId].name}`); |
| 376 | return true; |
| 377 | } |
| 378 | |
| 379 | // Example usage |
| 380 | login("user1"); |
| 381 | const fileId = uploadFile( |
| 382 | "document.pdf", |
| 383 | 1024 * 1024, |
| 384 | "application/pdf", |
| 385 | false, |
| 386 | ["important", "document"], |
| 387 | ); |
| 388 | updateFileMetadata(fileId, { isPublic: true }); |
| 389 | shareFile(fileId, "user2"); |
| 390 | console.log(getFileData(fileId)); |
| 391 | console.log(getStorageStats()); |
| 392 | logout(); |
| 393 | |
| 394 | export { |
| 395 | deleteFile, |
| 396 | fileStorage, |
| 397 | getFileData, |
| 398 | getStorageStats, |
| 399 | login, |
| 400 | logout, |
| 401 | searchFiles, |
| 402 | shareFile, |
| 403 | updateFileMetadata, |
| 404 | uploadFile, |
| 405 | users, |
| 406 | }; |