File Storage

mail@pastecode.io avatar
unknown
java
a month ago
7.1 kB
1
Indexable
Never
import java.util.ArrayList;
import java.util.List;

// Class representing a User
public class User {
    private String id;
    private String name;
    private String email;
    private List<Permission> permissions;

    public User(String id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.permissions = new ArrayList<>();
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }

    public List<Permission> getPermissions() {
        return permissions;
    }

    public void addPermission(Permission permission) {
        permissions.add(permission);
    }
}

// Class representing a File
import java.util.List;

public class File {
    private String id;
    private String name;
    private String content;
    private List<FileVersion> versions;
    private User owner;

    public File(String id, String name, String content, User owner) {
        this.id = id;
        this.name = name;
        this.content = content;
        this.owner = owner;
        this.versions = new ArrayList<>();
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public List<FileVersion> getVersions() {
        return versions;
    }

    public void addVersion(FileVersion version) {
        versions.add(version);
    }

    public User getOwner() {
        return owner;
    }
}

// Class representing a FileVersion
public class FileVersion {
    private String versionId;
    private String content;
    private String timestamp;

    public FileVersion(String versionId, String content, String timestamp) {
        this.versionId = versionId;
        this.content = content;
        this.timestamp = timestamp;
    }

    public String getVersionId() {
        return versionId;
    }

    public String getContent() {
        return content;
    }

    public String getTimestamp() {
        return timestamp;
    }
}

// Class representing a Folder
import java.util.List;

public class Folder {
    private String id;
    private String name;
    private List<File> files;
    private List<Folder> subfolders;
    private User owner;

    public Folder(String id, String name, User owner) {
        this.id = id;
        this.name = name;
        this.owner = owner;
        this.files = new ArrayList<>();
        this.subfolders = new ArrayList<>();
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<File> getFiles() {
        return files;
    }

    public List<Folder> getSubfolders() {
        return subfolders;
    }

    public User getOwner() {
        return owner;
    }

    public void addFile(File file) {
        files.add(file);
    }

    public void removeFile(String fileName) {
        files.removeIf(file -> file.getName().equals(fileName));
    }

    public void addSubfolder(Folder folder) {
        subfolders.add(folder);
    }

    public void removeSubfolder(String folderName) {
        subfolders.removeIf(folder -> folder.getName().equals(folderName));
    }
}

// Class representing a Permission
public class Permission {
    private User user;
    private File file;
    private boolean canRead;
    private boolean canWrite;

    public Permission(User user, File file, boolean canRead, boolean canWrite) {
        this.user = user;
        this.file = file;
        this.canRead = canRead;
        this.canWrite = canWrite;
    }

    public User getUser() {
        return user;
    }

    public File getFile() {
        return file;
    }

    public boolean isCanRead() {
        return canRead;
    }

    public boolean isCanWrite() {
        return canWrite;
    }
}

// Class representing the File Storage System
public class FileStorageSystem {
    private List<User> users;
    private List<Folder> folders;

    public FileStorageSystem() {
        this.users = new ArrayList<>();
        this.folders = new ArrayList<>();
    }

    public void addUser(User user) {
        users.add(user);
    }

    public void addFolder(Folder folder) {
        folders.add(folder);
    }

    public void uploadFile(User user, Folder folder, File file) {
        if (folder.getOwner().equals(user)) {
            folder.addFile(file);
        } else {
            System.out.println("User does not have permission to upload files to this folder.");
        }
    }

    public String downloadFile(User user, File file) {
        Permission permission = findPermission(user, file);
        if (permission != null && permission.isCanRead()) {
            return file.getContent();
        } else {
            return "Permission denied.";
        }
    }

    public void shareFile(User owner, File file, User targetUser, boolean canRead, boolean canWrite) {
        if (file.getOwner().equals(owner)) {
            Permission permission = new Permission(targetUser, file, canRead, canWrite);
            targetUser.addPermission(permission);
        } else {
            System.out.println("User does not have permission to share this file.");
        }
    }

    private Permission findPermission(User user, File file) {
        for (Permission permission : user.getPermissions()) {
            if (permission.getFile().equals(file)) {
                return permission;
            }
        }
        return null;
    }
}

// Example Main Application
public class FileStorageApp {
    public static void main(String[] args) {
        FileStorageSystem fileSystem = new FileStorageSystem();

        // Create users
        User user1 = new User("1", "Alice", "alice@example.com");
        User user2 = new User("2", "Bob", "bob@example.com");
        fileSystem.addUser(user1);
        fileSystem.addUser(user2);

        // Create folders
        Folder rootFolder = new Folder("root", "Root Folder", user1);
        Folder docsFolder = new Folder("docs", "Documents", user1);
        fileSystem.addFolder(rootFolder);
        fileSystem.addFolder(docsFolder);
        rootFolder.addSubfolder(docsFolder);

        // Create files
        File file1 = new File("file1", "file1.txt", "This is file 1 content.", user1);
        File file2 = new File("file2", "file2.txt", "This is file 2 content.", user1);
        fileSystem.uploadFile(user1, docsFolder, file1);
        fileSystem.uploadFile(user1, docsFolder, file2);

        // Share file with another user
        fileSystem.shareFile(user1, file1, user2, true, false);

        // Download file
        System.out.println("Content of file1.txt for Bob: " + fileSystem.downloadFile(user2, file1));

        // Attempt to download a file without permission
        System.out.println("Content of file2.txt for Bob: " + fileSystem.downloadFile(user2, file2));
    }
}
Leave a Comment