public async Task <SlideShowPictureDto> UpdateAsync(string fileName, SlideShowPictureDto dto)
        {
            if (string.IsNullOrWhiteSpace(fileName) ||
                !ImageManipulator.IsImageStream(dto.Base64Picture))
            {
                throw new ArgumentException();
            }

            var b64Part = (dto.Base64Picture == null)? null : GetBase64Part(dto.Base64Picture);

            var file = await BlobContainer.GetAllBytesOrNullAsync(fileName) ?? throw new FileNotFoundException();

            var xmlDoc   = new XmlDocument();
            var settings = GetReaderSettings();

            using var ms     = new MemoryStream(file);
            using var reader = XmlReader.Create(ms, settings);

            xmlDoc.Load(reader);

            var slideShowElm = xmlDoc.DocumentElement;
            var imgElm       = slideShowElm.FirstChild;
            var b64txtNode   = imgElm.FirstChild as XmlText;

            b64txtNode.InnerText = b64Part ?? b64txtNode.InnerText;
            slideShowElm.SetAttribute("uri", (dto.Uri == null) ? slideShowElm.GetAttribute("uri") : dto.Uri.ToString());
            slideShowElm.SetAttribute("name", dto.Name ?? slideShowElm.GetAttribute("name"));

            var bytes = Encoding.UTF8.GetBytes(xmlDoc.InnerXml);
            await BlobContainer.SaveAsync(fileName, bytes, true);

            return(await GetAsync(fileName));
        }
        public async Task <SlideShowPictureDto> GetAsync(string name)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException();
            }

            var buffer = await BlobContainer.GetAllBytesOrNullAsync(name) ?? throw new FileNotFoundException();

            var xmlStr = Encoding.UTF8.GetString(buffer);
            var xmlDoc = CreateXmlDoc();

            xmlDoc.LoadXml(xmlStr);

            var slideShowElm = xmlDoc.DocumentElement;
            var imgElm       = slideShowElm.FirstChild;
            var b64txtNode   = imgElm.FirstChild as XmlText;
            var imgTypeAttr  = imgElm.Attributes.Item(0);

            if (imgTypeAttr.LocalName != "imgType")
            {
                throw new Exception();
            }

            var dto = new SlideShowPictureDto {
                Name          = slideShowElm.GetAttribute("name"),
                Uri           = slideShowElm.GetAttribute("uri"),
                Base64Picture = SetB64ImgContentType(b64txtNode.InnerText, imgTypeAttr.InnerText),
                FileName      = name
            };

            return(dto);
        }
        public async Task <SlideShowPictureDto> SaveXmlAsync(SlideShowPictureDto dto)
        {
            if (string.IsNullOrWhiteSpace(dto.Base64Picture) || !ImageManipulator.IsImageStream(dto.Base64Picture))
            {
                throw new ArgumentException();
            }
            var base64Part = GetBase64Part(dto.Base64Picture);
            var buffer     = ImageManipulator.ResizeImage(base64Part, Options.Width, Options.Height, Options.ImageFormat);

            try
            {
                var xmlDoc       = CreateXmlDoc();
                var slideShowElm = xmlDoc.DocumentElement;
                var imgElm       = slideShowElm.FirstChild;
                var imgTypeAttr  = xmlDoc.CreateAttribute("imgType");

                imgTypeAttr.Value = Options.ImageFormat.ToString();
                slideShowElm.SetAttribute("name", dto.Name);
                slideShowElm.SetAttribute("uri", (dto.Uri == null)? "" : dto.Uri.ToString());

                imgElm.Attributes.Append(imgTypeAttr);
                imgElm.AppendChild(xmlDoc.CreateTextNode(buffer));

                var name = await SaveAsync(xmlDoc.OuterXml);

                return(await GetAsync(name));
            }
            //catch(XmlException ex)
            //{

            //}
            //catch(XmlSchemaValidationException ex)
            //{

            //}
            catch (Exception ex)
            {
                throw ex;
            }
        }
 public async Task <SlideShowPictureDto> Update([FromRoute] string fileName, [FromBody] SlideShowPictureDto dto)
 {
     return(await SlideShowAppService.UpdateAsync(fileName, dto));
 }
 public async Task <SlideShowPictureDto> Create([FromBody] SlideShowPictureDto dto)
 {
     return(await SlideShowAppService.SaveAsync(dto));
 }
示例#6
0
 public async Task <SlideShowPictureDto> UpdateAsync(string fileName, SlideShowPictureDto dto)
 {
     return(await SlideShowPictureManager.UpdateAsync(fileName, dto));
 }
示例#7
0
 public async Task <SlideShowPictureDto> SaveAsync(SlideShowPictureDto dto)
 {
     return(await SlideShowPictureManager.SaveXmlAsync(dto));
 }