public async Task <ActionResult <List <FileVersion> > > AddVersions(string resourceName, [FromBody] List <FileVersion> fileVersions)
        {
            if (fileVersions == null)
            {
                return(BadRequest(new { error = "Couldn't parse the versions to add" }));
            }

            var body = await Request.Body.GetStringFromStreamAsync(Encoding.UTF8);

            var errors = CheckVersionsForErrors(fileVersions);

            if (errors != null)
            {
                logger.LogError($"POST/name Couldn't parse body {body}");
                return(BadRequest(errors));
            }

            File f = null;

            await foreach (var item in storageRepository.GetFilesAsync(resourceName))
            {
                // Return an error if there's more than one file with the same name
                if (f != null)
                {
                    logger.LogCritical($"Two files with the same resource name exist : {resourceName}");
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
                f = item;
            }

            try
            {
                f.Versions.AddRange(fileVersions);

                if (await storageRepository.UpdateAsync(f.Id, f))
                {
                    return(Created($"/files/{f.ResourceName}/[versions]", f));
                }
                else
                {
                    logger.LogError($"Couldn't add the versions to file {f.Name} with versions {fileVersions}");
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            catch (Exception e)
            {
                logger.LogError($"Error in POST/files : Exception thrown with body  {body} and error {e.Message}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Example #2
0
        public async Task <Response <int> > Handle(UpdateContentCommand command, CancellationToken cancellationToken)
        {
            Item item = await _storageRepository.GetByIdAsync(command.Id);

            if (item == null)
            {
                throw new ApiException($"Content Not Found.");
            }
            else
            {
                item.Name           = command.Name ?? Path.GetFileNameWithoutExtension(command.File.FileName);
                item.ContentType    = command.File.GetContentType();
                item.LastModifiedBy = _httpContext.HttpContext.User.Identity.Name;
                item.Description    = command.Description;
                item.Size           = command.File.Length;
                item.VerifiedHash   = await command.File
                                      .CalculateMD5FileHashAsync(cancellationToken);

                item.Url = await _storageFileSystemProvider
                           .StoreAsync(command.File, cancellationToken, overwrite : true).ConfigureAwait(false);

                if (!string.IsNullOrWhiteSpace(item.Url))
                {
                    await _storageRepository.UpdateAsync(item);
                }

                _logger.LogInformation("storage item {Name} successfully updated in {Url}", item.Name, item.Url);
                return(new Response <int>
                {
                    Data = item.Id,
                    Succeeded = true,
                    Message = $"storage item {item.Name} successfully updated."
                });
            }
        }