예제 #1
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);
        }