コード例 #1
0
        public async Task <CommonResponse> PostSharingPhotos([Required] string musicid, [Required] IList <IFormFile> files)
        {
            if (files.Count == 0)
            {
                return new CommonResponse {
                           StatusCode = -2
                }
            }
            ;                                                                   // Didn't place files into form-data correctly.
            var sharing    = _resolver.GetSharing();
            var folderPath = Path.Combine(sharing, musicid);
            var folderInfo = new DirectoryInfo(folderPath);

            if (!folderInfo.Exists)
            {
                folderInfo.Create();
            }
            var received         = new List <string>();
            var receivedFullPath = new List <string>();

            try
            {
                foreach (var file in files)
                {
                    var fileName = $"{Guid.NewGuid().ToString()}.{StaticFilePathResolver.GetFileExtension(file.FileName)}";
                    var filePath = Path.Combine(folderPath, fileName);
                    using (var fs = System.IO.File.Create(filePath))
                    {
                        await file.OpenReadStream().CopyToAsync(fs);
                    }
                    received.Add(fileName);
                    receivedFullPath.Add(filePath);
                }
                return(new UploadPostPhotosResponse {
                    StatusCode = 0, ReceivedFiles = received
                });
            }

            catch (Exception ex)
            {
                _logger.LogError($"Write file error: {ex.Message} {ex.StackTrace}");
                // Regards this upload as failed. Need to free up all space taken up by this failed upload.
                foreach (var filePath in receivedFullPath)
                {
                    var file = new FileInfo(filePath);
                    if (file.Exists)
                    {
                        file.Delete();
                    }
                }
                return(new CommonResponse {
                    StatusCode = -1
                });                                            // Server-side error.
            }
        }
コード例 #2
0
        public IActionResult GetSharingPhotos([FromQuery][Required] string musicid, [FromQuery][Required] string photo)
        {
            var ext      = StaticFilePathResolver.GetFileExtension(photo);
            var filePath = Path.Combine(_resolver.GetSharing(), musicid, photo);
            var file     = new FileInfo(filePath);

            if (file.Exists)
            {
                return(PhysicalFile(file.FullName, $"image/{ext}"));
            }
            return(NotFound());
        }
コード例 #3
0
        public IActionResult GetAvatar([FromQuery][Required] string avatar)
        {
            var path = Path.Combine(_resolver.GetAvatar(), avatar);
            var ext  = StaticFilePathResolver.GetFileExtension(avatar);
            var file = new FileInfo(path);

            if (file.Exists)
            {
                return(PhysicalFile(file.FullName, $"image/{ext}"));
            }
            return(NotFound());
        }