Exemple #1
0
        public async Task <bool> Delete([FromQuery] DeleteStoredFileInput input)
        {
            var ownerSpecified =
                !string.IsNullOrWhiteSpace(input.OwnerType) && !string.IsNullOrWhiteSpace(input.OwnerId);

            if (string.IsNullOrWhiteSpace(input.OwnerId) && !string.IsNullOrWhiteSpace(input.OwnerType))
            {
                ModelState.AddModelError(nameof(input.OwnerId), $"{nameof(input.OwnerId)} must not be null when {nameof(input.OwnerType)} is specified");
            }

            if (string.IsNullOrWhiteSpace(input.OwnerType) && !string.IsNullOrWhiteSpace(input.OwnerId))
            {
                ModelState.AddModelError(nameof(input.OwnerType), $"{nameof(input.OwnerType)} must not be null when {nameof(input.OwnerId)} is specified");
            }

            if (input.FileId == null && string.IsNullOrWhiteSpace(input.PropertyName))
            {
                ModelState.AddModelError(nameof(input.FileId), $"Id must not be null");
            }

            var owner = ownerSpecified
                ? await _dynamicRepository.GetAsync(input.OwnerType, input.OwnerId)
                : null;

            if (ownerSpecified && owner == null)
            {
                ModelState.AddModelError(input.OwnerId, $"Owner not found (type = '{input.OwnerType}', id = '{input.OwnerId}')");
            }

            var processAsProperty = owner != null && !string.IsNullOrWhiteSpace(input.PropertyName);
            var property          = processAsProperty
                ? ReflectionHelper.GetProperty(owner, input.PropertyName)
                : null;

            if (processAsProperty)
            {
                if (property == null)
                {
                    ModelState.AddModelError(nameof(input.PropertyName), $"Property '{owner.GetType().Name}.{input.PropertyName}' not found");
                }

                if (property != null && !typeof(StoredFile).IsAssignableFrom(property.PropertyType))
                {
                    ModelState.AddModelError(nameof(input.PropertyName), $"Wrong type of '{owner.GetType().Name}.{input.PropertyName}' property (actual: '{property.PropertyType.FullName}', expected: '{nameof(StoredFile)}')");
                }
            }

            if (!ModelState.IsValid)
            {
                throw new AbpValidationException("Failed to delete file", GetValidationResults(ModelState));
            }

            var storedFile = input.FileId != null
                ? await _fileRepository.GetAll().FirstOrDefaultAsync(f => f.Id == input.FileId.Value)
                : property != null
                    ? property.GetValue(owner) as StoredFile
                    : null;

            if (storedFile != null)
            {
                // if the `property` is specified - then it's direct link and we should set it to null
                if (property != null)
                {
                    property.SetValue(owner, null, null);
                    await _dynamicRepository.SaveOrUpdateAsync(owner);
                }

                await _fileService.DeleteAsync(storedFile);
            }

            return(true);
        }