Ejemplo n.º 1
0
        public async Task <ActionResult> UploadProductMedium(Guid productId, [FromQuery] ProductMediaParameter parameter, List <IFormFile> files)
        {
            if (files is null || parameter is null)
            {
                return(BadRequest("File is NULL or parameter is NULL"));
            }

            long size = files.Sum(x => x.Length);

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    var fileName       = DateTime.UtcNow.ToLocalTime().ToString("yyyyMMddHHmmssffff");
                    var fileExtension  = Path.GetExtension(file.FileName).Substring(1);
                    var fileNameToSave = String.Join(".", fileName, fileExtension);

                    var mediaInformationToDB = new ProductMedia()
                    {
                        FileName      = fileName,
                        FileExtension = fileExtension,
                        ProductId     = productId,
                        Type          = parameter.Type
                    };

                    _productMediaService.CreateProductMedia(mediaInformationToDB);

                    await file.SaveFileAsync(parameter.Type, fileNameToSave);
                }
            }

            await _productMediaService.SaveAsync();

            return(Ok(new { count = files.Count, size }));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> GetProductMedia(Guid productId, Guid mediaId, [FromQuery] ProductMediaParameter parameter)
        {
            var productMediaInfo = await _productMediaService.GetProductMediaAsync(productId, mediaId, false);

            if (productMediaInfo is not null)
            {
                var fileResult = FileExtension.ConvertFileToBase64String(productMediaInfo, parameter.Type);

                return(Ok(JsonConvert.SerializeObject(fileResult)));
            }

            return(NotFound());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetProductMedium(Guid categoryId, Guid productId, [FromQuery] ProductMediaParameter parameter)
        {
            var productMediumInfo = await _productMediaService.GetMediumForProductAsync(productId,
                                                                                        parameter.Type,
                                                                                        trackChanges : false);

            var fileConvertedResult = new List <string>();

            if (productMediumInfo is not null)
            {
                foreach (var productMediaInfo in productMediumInfo)
                {
                    var fileResult = FileExtension.ConvertFileToBase64String(productMediaInfo, parameter.Type);
                    fileConvertedResult.Add(fileResult);
                }
            }

            if (fileConvertedResult is not null)
            {
                return(Ok(fileConvertedResult));
            }
            else
            {
                return(NotFound());
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> DeleteProductMedia(Guid categoryId, Guid productId, Guid mediaId, [FromQuery] ProductMediaParameter parameter)
        {
            var productMedia = await _productMediaService.GetProductMediaAsync(productId, mediaId, false);

            if (productMedia is null)
            {
                return(NotFound($"Doesn't exist media with id: {mediaId} in the database"));
            }
            var path         = FileExtension.GetTruePath(parameter.Type);
            var fullFileName = String.Join(".", productMedia.FileName, productMedia.FileExtension);
            var fullPath     = Path.Combine(path, fullFileName);

            if (System.IO.File.Exists(fullPath))
            {
                System.IO.File.Delete(fullPath);

                _productMediaService.DeleteProductMedia(productMedia);
                await _productMediaService.SaveAsync();

                return(NoContent());
            }
            else
            {
                return(NotFound("File not found in the media server"));
            }
        }