Esempio n. 1
0
        public async Task <ChatRoom> JoinRoom(string roomId, string password = null)
        {
            var room = await database.ChatRoomRepository.FindRoomWithConnectionsAndMessages(roomId);

            if (room == null)
            {
                return(null);
            }

            if (room.IsPassword && room.Password != password)
            {
                ErrorWriter <string> .Append(roomId, "Invalid chat room password");

                return(null);
            }

            if (room.MaxClients != null && room.Connections.Count + 1 > room.MaxClients)
            {
                ErrorWriter <string> .Append(roomId, $"Max clients number is: {room.MaxClients}");

                return(null);
            }

            return(room);
        }
Esempio n. 2
0
        public async Task <bool> UploadFiles(IEnumerable <IFormFile> files, string directoryId = null, bool isPrivate = false)
        {
            if (!files.Any())
            {
                ErrorWriter.Append("You have to choose files to upload");
                return(false);
            }

            bool uploadResult = true;

            foreach (var file in files)
            {
                uploadResult = uploadResult && await UploadFile(file, directoryId, isPrivate);
            }

            return(uploadResult);
        }
Esempio n. 3
0
        public async Task <bool> UploadFile(IFormFile file, string directoryId = null, bool isPrivate = false)
        {
            if (file == null)
            {
                ErrorWriter.Append("You have to choose file to upload");
                return(false);
            }

            if (await storageSizeManager.CountStorageSize(isPrivateStorage: isPrivate) + (uint)file.Length / StorageSizeManager.UnitConversionMultiplier
                > (!isPrivate ? storageSizeManager.MaxPublicStorageSizeInGb : storageSizeManager.MaxPrivateStorageSizeInGb) * Math.Pow(StorageSizeManager.UnitConversionMultiplier, 2))
            {
                Alertify.Push("Storage is full", AlertType.Warning);
                return(false);
            }

            string filePath = !isPrivate ? @"public/" : $@"private/{currentUserId}/";

            if (!string.IsNullOrEmpty(directoryId))
            {
                var directory = await database.DirectoryRepository.FindDirectoryWithParent(directoryId);

                filePath = await filePathBuilder.BuildFilePath(directory, filePath);
            }

            var uploadedFile = await fileWriter.Upload(file, filePath);

            if (uploadedFile != null)
            {
                var fileToStore = new FileBuilder()
                                  .SetName(file.FileName)
                                  .SetPath(uploadedFile.Path)
                                  .WithSize(uploadedFile.Size)
                                  .InDirectory(directoryId)
                                  .AssignedTo(userId: !isPrivate ? null : currentUserId)
                                  .Build();

                database.FileRepository.Add(fileToStore);

                return(await database.Complete());
            }

            Alertify.Push("File cannot be uploaded", AlertType.Error);
            return(false);
        }
Esempio n. 4
0
        public async Task <Directory> CreateDirectory(string name, string directoryPath, bool isPrivate = false, string parentDirectoryId = null)
        {
            string fullPath = $"{directoryPath}{name}";

            if (!fileWriter.CreateDirectory(fullPath))
            {
                ErrorWriter.Append("Directory name already exists");
                return(null);
            }

            var directory = new DirectoryBuilder()
                            .SetName(name)
                            .SetPath(fullPath)
                            .AssignedTo(!isPrivate ? null : currentUserId)
                            .WithParentDirectory(parentDirectoryId)
                            .Build();

            database.DirectoryRepository.Add(directory);

            return(await database.Complete() ? directory : null);
        }