|
| 1 | +import Bool "mo:base/Bool"; |
| 2 | +import Array "mo:base/Array"; |
| 3 | +import Blob "mo:base/Blob"; |
| 4 | +import HashMap "mo:map/Map"; |
| 5 | +import { phash; thash } "mo:map/Map"; |
| 6 | +import Iter "mo:base/Iter"; |
| 7 | +import Nat "mo:base/Nat"; |
| 8 | +import Principal "mo:base/Principal"; |
| 9 | +import Text "mo:base/Text"; |
| 10 | +import Option "mo:base/Option"; |
| 11 | + |
| 12 | +persistent actor Filevault { |
| 13 | + |
| 14 | + // Define a data type for a file's chunks. |
| 15 | + type FileChunk = { |
| 16 | + chunk : Blob; |
| 17 | + index : Nat; |
| 18 | + }; |
| 19 | + |
| 20 | + // Define a data type for a file's data. |
| 21 | + type File = { |
| 22 | + name : Text; |
| 23 | + chunks : [FileChunk]; |
| 24 | + totalSize : Nat; |
| 25 | + fileType : Text; |
| 26 | + }; |
| 27 | + |
| 28 | + // Define a data type for storing files associated with a user principal. |
| 29 | + type UserFiles = HashMap.Map<Text, File>; |
| 30 | + |
| 31 | + // HashMap to store the user data |
| 32 | + private var files = HashMap.new<Principal, UserFiles>(); |
| 33 | + |
| 34 | + // Return files associated with a user's principal. |
| 35 | + private func getUserFiles(user : Principal) : UserFiles { |
| 36 | + switch (HashMap.get(files, phash, user)) { |
| 37 | + case null { |
| 38 | + let newFileMap = HashMap.new<Text, File>(); |
| 39 | + let _ = HashMap.put(files, phash, user, newFileMap); |
| 40 | + newFileMap; |
| 41 | + }; |
| 42 | + case (?existingFiles) existingFiles; |
| 43 | + }; |
| 44 | + }; |
| 45 | + |
| 46 | + // Check if a file name already exists for the user. |
| 47 | + public shared (msg) func checkFileExists(name : Text) : async Bool { |
| 48 | + Option.isSome(HashMap.get(getUserFiles(msg.caller), thash, name)); |
| 49 | + }; |
| 50 | + |
| 51 | + // Upload a file in chunks. |
| 52 | + public shared (msg) func uploadFileChunk(name : Text, chunk : Blob, index : Nat, fileType : Text) : async () { |
| 53 | + let userFiles = getUserFiles(msg.caller); |
| 54 | + let fileChunk = { chunk = chunk; index = index }; |
| 55 | + |
| 56 | + switch (HashMap.get(userFiles, thash, name)) { |
| 57 | + case null { |
| 58 | + let _ = HashMap.put(userFiles, thash, name, { name = name; chunks = [fileChunk]; totalSize = chunk.size(); fileType = fileType }); |
| 59 | + }; |
| 60 | + case (?existingFile) { |
| 61 | + let updatedChunks = Array.append(existingFile.chunks, [fileChunk]); |
| 62 | + let _ = HashMap.put( |
| 63 | + userFiles, |
| 64 | + thash, |
| 65 | + name, |
| 66 | + { |
| 67 | + name = name; |
| 68 | + chunks = updatedChunks; |
| 69 | + totalSize = existingFile.totalSize + chunk.size(); |
| 70 | + fileType = fileType; |
| 71 | + } |
| 72 | + ); |
| 73 | + }; |
| 74 | + }; |
| 75 | + }; |
| 76 | + |
| 77 | + // Return list of files for a user. |
| 78 | + public shared (msg) func getFiles() : async [{ name : Text; size : Nat; fileType : Text }] { |
| 79 | + Iter.toArray( |
| 80 | + Iter.map( |
| 81 | + HashMap.vals(getUserFiles(msg.caller)), |
| 82 | + func(file : File) : { name : Text; size : Nat; fileType : Text } { |
| 83 | + { |
| 84 | + name = file.name; |
| 85 | + size = file.totalSize; |
| 86 | + fileType = file.fileType; |
| 87 | + }; |
| 88 | + } |
| 89 | + ) |
| 90 | + ); |
| 91 | + }; |
| 92 | + |
| 93 | + // Return total chunks for a file |
| 94 | + public shared (msg) func getTotalChunks(name : Text) : async Nat { |
| 95 | + switch (HashMap.get(getUserFiles(msg.caller), thash, name)) { |
| 96 | + case null 0; |
| 97 | + case (?file) file.chunks.size(); |
| 98 | + }; |
| 99 | + }; |
| 100 | + |
| 101 | + // Return specific chunk for a file. |
| 102 | + public shared (msg) func getFileChunk(name : Text, index : Nat) : async ?Blob { |
| 103 | + switch (HashMap.get(getUserFiles(msg.caller), thash, name)) { |
| 104 | + case null null; |
| 105 | + case (?file) { |
| 106 | + switch (Array.find(file.chunks, func(chunk : FileChunk) : Bool { chunk.index == index })) { |
| 107 | + case null null; |
| 108 | + case (?foundChunk) ?foundChunk.chunk; |
| 109 | + }; |
| 110 | + }; |
| 111 | + }; |
| 112 | + }; |
| 113 | + |
| 114 | + // Get file's type. |
| 115 | + public shared (msg) func getFileType(name : Text) : async ?Text { |
| 116 | + switch (HashMap.get(getUserFiles(msg.caller), thash, name)) { |
| 117 | + case null null; |
| 118 | + case (?file) ?file.fileType; |
| 119 | + }; |
| 120 | + }; |
| 121 | + |
| 122 | + // Delete a file. |
| 123 | + public shared (msg) func deleteFile(name : Text) : async Bool { |
| 124 | + Option.isSome(HashMap.remove(getUserFiles(msg.caller), thash, name)); |
| 125 | + }; |
| 126 | +}; |
0 commit comments