Commit b2d025f5 authored by alan.f's avatar alan.f

clean-code

parent ed89adb7
...@@ -23,19 +23,29 @@ namespace SendFileWebapi.Controllers ...@@ -23,19 +23,29 @@ namespace SendFileWebapi.Controllers
static Logger logger = LogManager.GetCurrentClassLogger(); static Logger logger = LogManager.GetCurrentClassLogger();
[HttpPost] [HttpPost]
public async Task Post([FromForm]Models.InputsDto inputsDto) public async Task Post([FromForm]Models.FilesPackage inputsDto)
{ {
var clone = inputsDto.UplodedFiles.Select(f =>
{
var s = new MemoryStream();
f.CopyTo(s);
s.Seek(0, SeekOrigin.Begin);
return new { f.FileName, s };
}).ToArray();
var client = new DicomClient(inputsDto.ServerHost, inputsDto.Port, false, inputsDto.ServerAET, inputsDto.Aet); var client = new DicomClient(inputsDto.ServerHost, inputsDto.Port, false, inputsDto.ServerAET, inputsDto.Aet);
client.NegotiateAsyncOps(); client.NegotiateAsyncOps();
inputsDto.FileNames.ToObservable() clone.ToObservable()
.Select(fileName => Observable.FromAsync(async () => .Select(uploadedFile => Observable.FromAsync(async () =>
{ {
var stream = fileName.OpenReadStream(); using (var stream = uploadedFile.s)
var request = new Dicom.Network.DicomCStoreRequest(await DicomFile.OpenAsync(stream, FileReadOption.ReadAll)); {
var request = new Dicom.Network.DicomCStoreRequest(await DicomFile.OpenAsync(stream));
await client.AddRequestAsync(request); await client.AddRequestAsync(request);
await client.SendAsync(); await client.SendAsync();
stream.Dispose(); }
}).Select(x => fileName.FileName).Catch<string, Exception>(exp => throw new Exception($"failed to send: {fileName}, with error:{exp.Message}"))) }).Select(x => uploadedFile.FileName).Catch<string, Exception>(exp => throw new Exception($"failed to send: {uploadedFile}, with error:{exp.Message}")))
.Concat() .Concat()
.Subscribe( .Subscribe(
filename => filename =>
......
...@@ -38,26 +38,26 @@ namespace SendFileWebapi.Controllers ...@@ -38,26 +38,26 @@ namespace SendFileWebapi.Controllers
.ToArray(); .ToArray();
} }
[HttpPost] //[HttpPost]
public async Task<IActionResult> Post([FromForm]Models.InputsDto inputsDto) //public async Task<IActionResult> Post([FromForm]Models.InputsDto inputsDto)
{ //{
var client = new DicomClient(inputsDto.ServerHost, inputsDto.Port, false, inputsDto.ServerAET, inputsDto.Aet); // var client = new DicomClient(inputsDto.ServerHost, inputsDto.Port, false, inputsDto.ServerAET, inputsDto.Aet);
foreach (var formFile in inputsDto.FileNames) // foreach (var formFile in inputsDto.FileNames)
{ // {
FileStream fs=null; // FileStream fs=null;
if (formFile.Length > 0) // if (formFile.Length > 0)
using (var stream = new FileStream(formFile.FileName, FileMode.Create,FileAccess.Write)) // using (var stream = new FileStream(formFile.FileName, FileMode.Create,FileAccess.Write))
{ // {
formFile.CopyTo(stream); // formFile.CopyTo(stream);
fs = stream; // fs = stream;
} // }
var request = new Dicom.Network.DicomCStoreRequest(fs.Name); // var request = new Dicom.Network.DicomCStoreRequest(fs.Name);
await client.AddRequestAsync(request); // await client.AddRequestAsync(request);
await client.SendAsync(); // await client.SendAsync();
} // }
return null; // return null;
} //}
} }
} }
...@@ -6,11 +6,11 @@ using System.Threading.Tasks; ...@@ -6,11 +6,11 @@ using System.Threading.Tasks;
namespace SendFileWebapi.Models namespace SendFileWebapi.Models
{ {
public class InputsDto public class FilesPackage
{ {
public string ServerHost { get; set; } public string ServerHost { get; set; }
public int Port { get; set; } public int Port { get; set; }
public IFormFileCollection FileNames { get; set; } public IFormFileCollection UplodedFiles { get; set; }
public string ServerAET { get; set; } public string ServerAET { get; set; }
public string Aet { get; set; } public string Aet { get; set; }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment