Esempio n. 1
0
        public async Task <EntityResponse <UploadOutputDto> > Upload(IFormFile file)
        {
            if (file != null && file.Length > 0)
            {
                var localName = file.FileName;
                var ext       = Path.GetExtension(localName);
                var md5Byte   = new MD5CryptoServiceProvider().ComputeHash(file.OpenReadStream());
                var md5       = System.BitConverter.ToString(md5Byte).Replace("-", "").ToLower();
                var folder    = Path.Combine(FilePath, md5.Substring(0, 2));
                if (!Directory.Exists(folder))
                {
                    DirectoryInfo di = new DirectoryInfo(folder);
                    di.Create();
                }
                var targetPath = Path.Combine(FilePath, md5.Substring(0, 2), $"{md5}{ext}");

                if (System.IO.File.Exists(targetPath))
                {
                    if (_artworkAppService.TryGetArtworkItem(md5, localName, out var dto))
                    {
                        return(EntityResponse <UploadOutputDto> .Ok(dto));
                    }
                }

                try
                {
                    using (var stream = System.IO.File.Create(targetPath))
                    {
                        await file.CopyToAsync(stream);
                    }

                    var artworkItem = await _artworkAppService.CreateArtworkItem(new ArtworkItemCreateInputDto
                    {
                        Md5        = md5,
                        LocalName  = localName,
                        RelatedUrl = targetPath,
                    });

                    return(new EntityResponse <UploadOutputDto>
                    {
                        Code = Code.Ok,
                        Data = new UploadOutputDto
                        {
                            Uid = artworkItem.Id,
                            Status = UploadStatus.Done,
                            Md5 = md5,
                            LocalName = artworkItem.LocalName,
                            RelatedUrl = artworkItem.RelatedUrl,
                            AbsoluteUrl = artworkItem.AbsoluteUrl,
                        },
                    });
                }
                catch (Exception ex)
                {
                    return(new EntityResponse <UploadOutputDto>
                    {
                        Code = Code.Error,
                        Data = new UploadOutputDto
                        {
                            Status = UploadStatus.Error,
                        },
                        Message = "server error"
                    });
                }
            }

            throw new Exception("must upload a file");
        }
Esempio n. 2
0
        public async Task <EntityResponse <ArtworkDetailOutputDto> > GetDetail(int id)
        {
            var result = await _artworkAppService.GetDetail(id);

            return(EntityResponse <ArtworkDetailOutputDto> .Ok(result));
        }
Esempio n. 3
0
        public async Task <EntityResponse <int> > CreateArtwork([FromBody] ArtworkCreateInputDto inputDto)
        {
            var result = await _artworkAppService.CreateArtwork(inputDto);

            return(EntityResponse <int> .Ok(result));
        }
Esempio n. 4
0
        public async Task <EntityResponse <PersonalIntroDto> > Get()
        {
            var result = await _personalIntroAppService.Get();

            return(EntityResponse <PersonalIntroDto> .Ok(result));
        }