public StorageDrive AddStorageDrive(StorageDrive drive) { Guid?organizationId = _organizationManager.GetDefaultOrganization().Id; var existingDrive = _storageDriveRepository.Find(null).Items?.Where(q => q.OrganizationId == organizationId && q.Name.ToLower() == drive.Name.ToLower()).FirstOrDefault(); if (existingDrive != null) { throw new EntityAlreadyExistsException($"Drive {drive.Name} already exists within this organization"); } CheckDefaultDrive(drive, organizationId); var adapterType = drive.FileStorageAdapterType; if (string.IsNullOrEmpty(adapterType)) { adapterType = AdapterType.LocalFileStorage.ToString(); } //check if a new drive can be created for the current organization long?maxSizeInBytes = drive.MaxStorageAllowedInBytes; //size of new drive long?organizationStorage = GetTotalOrganizationStorage(organizationId); //sum of all drives for the current organization long?orgMaxSizeInBytes = _organizationManager.GetMaxStorageInBytes(organizationId); //max allowed storage for the current organization long?updatedOrgStorage = maxSizeInBytes + organizationStorage; //sum of new drive and all existing drives if (orgMaxSizeInBytes != null && maxSizeInBytes > orgMaxSizeInBytes) { throw new EntityOperationException("Drive size would exceed the allowed storage space for this organization"); } if (string.IsNullOrEmpty(drive.StoragePath)) { drive.StoragePath = drive.Name; } if (drive.MaxStorageAllowedInBytes == null) { drive.MaxStorageAllowedInBytes = orgMaxSizeInBytes; } if (string.IsNullOrEmpty(drive.FileStorageAdapterType)) { drive.FileStorageAdapterType = AdapterType.LocalFileStorage.ToString(); } if (drive.IsDefault == null) { drive.IsDefault = false; } var storageDrive = new StorageDrive() { Name = drive.Name, FileStorageAdapterType = drive.FileStorageAdapterType, OrganizationId = organizationId, StoragePath = drive.StoragePath, CreatedBy = _httpContextAccessor.HttpContext.User.Identity.Name, CreatedOn = DateTime.UtcNow, StorageSizeInBytes = drive.StorageSizeInBytes ?? 0, MaxStorageAllowedInBytes = drive.MaxStorageAllowedInBytes, IsDefault = drive.IsDefault }; _storageDriveRepository.Add(storageDrive); _webhookPublisher.PublishAsync("Files.NewDriveCreated", storageDrive.Id.ToString(), storageDrive.Name); return(storageDrive); }