/// <summary> /// Removes a file by uuid if the user is owner and the file exists /// </summary> /// <param name="fileUuid">The uuid of the file to remove</param> /// <param name="requestingUser">The user that made the request</param> public async Task Delete(Guid fileUuid, UserHelper requestingUser) { if (fileUuid == Guid.Empty) { throw new UnprocessableException(); } string directoryPath = FileHelper.GetDirectoryPathByFileUuid(fileUuid); string fullPath = FileHelper.GetFilePathByUuid(fileUuid); DirectoryInfoFile infoFile = await DirectoryHelper.GetInfoFileFromDirectory(directoryPath); FileContentInfo fileContentInfo = infoFile.FileInfo .Find(fi => fi.FileOwnerUuid == requestingUser.Uuid); bool fileCanBeRemovedByRequestingUser = fileContentInfo.FilesOwnedByUser .Contains(fileUuid) || requestingUser.AccountRole > AccountRole.User; if (!File.Exists(fullPath)) { throw new UnprocessableException(); } if (!fileCanBeRemovedByRequestingUser) { throw new UnauthorizedAccessException(); } File.Delete(fullPath); fileContentInfo.FilesOwnedByUser.Remove(fileUuid); await DirectoryHelper.UpdateInfoFile(directoryPath, infoFile); }
/// <summary> /// Gets the directory info file and returns a modified version which only contains /// a list of files owned by the user and if the user is owner of the directory /// </summary> /// <param name="path">The user specified path</param> /// <param name="userUuid">The uuid of the requesting user</param> /// <returns>an modified version which only contains /// a list of files owned by the user and if the user is owner of the directory</returns> public async Task <DirectoryInfoFile> GetDirectoryInfo(string path, Guid userUuid) { if (!DirectoryHelper.PathIsValid(path)) { throw new UnprocessableException(); } string fullPath = $"{Environment.CurrentDirectory}/Media{path}"; if (!File.Exists($"{fullPath}info.json")) { throw new FileNotFoundException(); } DirectoryInfoFile info = await DirectoryHelper.GetInfoFileFromDirectory(fullPath); info.FileInfo.RemoveAll(fi => fi.FileOwnerUuid != userUuid); info.DirectoryContentInfo.RemoveAll(dci => dci.OwnerUuid != userUuid); if (info.DirectoryOwnerUuid != userUuid) { info.DirectoryOwnerUuid = Guid.Empty; } return(info); }
/// <summary> /// Creates a directory if the following conditions are met: /// <list type="bullet"> /// <item> /// <description>The full path does not exists</description> /// </item> /// <item> /// <description>The max allowed sub folders for the full path is not reached</description> /// </item> /// </list> /// </summary> /// <param name="folder">The form the user send</param> /// <param name="requestingUserUuid">The uuid of the requesting user</param> public async Task CreateFolder(FolderUpload folder, Guid requestingUserUuid) { if (!Directory.Exists($"{Environment.CurrentDirectory}/Media{folder.ParentPath}") || !DirectoryHelper.CanCreateFolderInDirectory(folder.ParentPath)) { throw new UnprocessableException(); } string fullPath = $"{Environment.CurrentDirectory}/Media{folder.ParentPath}{folder.Name}"; if (Directory.Exists(fullPath)) { throw new DuplicateNameException(); } FilePath filepathInfo = FilePathInfo.Find(folder.ParentPath); string rootPath = $"{Environment.CurrentDirectory}/Media{filepathInfo?.Path}"; var rootDirectoryInfoFile = await DirectoryHelper.GetInfoFileFromDirectory(rootPath); DirectoryContentInfo directoryContentInfo = rootDirectoryInfoFile.DirectoryContentInfo .Find(dci => dci.OwnerUuid == requestingUserUuid); if (directoryContentInfo == null || directoryContentInfo == new DirectoryContentInfo()) { rootDirectoryInfoFile.DirectoryContentInfo.Add(new DirectoryContentInfo { DirectoriesOwnedByUser = new List <string> { folder.Name }, OwnerUuid = requestingUserUuid }); } else { directoryContentInfo.DirectoriesOwnedByUser.Add(folder.Name); } Directory.CreateDirectory(fullPath); var directoryInfoFile = new DirectoryInfoFile { DirectoryOwnerUuid = requestingUserUuid }; await DirectoryHelper.UpdateInfoFile(rootPath, rootDirectoryInfoFile); await DirectoryHelper.UpdateInfoFile(fullPath, directoryInfoFile); }
private static async Task UpdateInfoFileAfterFileUpload(Guid requestingUserUuid, string fullPath, List <Guid> fileNameCollection) { DirectoryInfoFile directoryInfoFile = await DirectoryHelper.GetInfoFileFromDirectory(fullPath); FileContentInfo fileInfo = directoryInfoFile.FileInfo.Find(fi => fi.FileOwnerUuid == requestingUserUuid); if (fileInfo != null) { fileInfo.FilesOwnedByUser.AddRange(fileNameCollection); } else { directoryInfoFile.FileInfo.Add(new FileContentInfo { FileOwnerUuid = requestingUserUuid, FilesOwnedByUser = fileNameCollection }); } await DirectoryHelper.UpdateInfoFile(fullPath, directoryInfoFile); }