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

clean-code

parent ed89adb7
......@@ -23,19 +23,29 @@ namespace SendFileWebapi.Controllers
static Logger logger = LogManager.GetCurrentClassLogger();
[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);
client.NegotiateAsyncOps();
inputsDto.FileNames.ToObservable()
.Select(fileName => Observable.FromAsync(async () =>
clone.ToObservable()
.Select(uploadedFile => Observable.FromAsync(async () =>
{
var stream = fileName.OpenReadStream();
var request = new Dicom.Network.DicomCStoreRequest(await DicomFile.OpenAsync(stream, FileReadOption.ReadAll));
using (var stream = uploadedFile.s)
{
var request = new Dicom.Network.DicomCStoreRequest(await DicomFile.OpenAsync(stream));
await client.AddRequestAsync(request);
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()
.Subscribe(
filename =>
......
......@@ -38,26 +38,26 @@ namespace SendFileWebapi.Controllers
.ToArray();
}
[HttpPost]
public async Task<IActionResult> Post([FromForm]Models.InputsDto inputsDto)
{
//[HttpPost]
//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)
{
FileStream fs=null;
if (formFile.Length > 0)
using (var stream = new FileStream(formFile.FileName, FileMode.Create,FileAccess.Write))
{
formFile.CopyTo(stream);
fs = stream;
}
var request = new Dicom.Network.DicomCStoreRequest(fs.Name);
await client.AddRequestAsync(request);
await client.SendAsync();
}
return null;
}
// foreach (var formFile in inputsDto.FileNames)
// {
// FileStream fs=null;
// if (formFile.Length > 0)
// using (var stream = new FileStream(formFile.FileName, FileMode.Create,FileAccess.Write))
// {
// formFile.CopyTo(stream);
// fs = stream;
// }
// var request = new Dicom.Network.DicomCStoreRequest(fs.Name);
// await client.AddRequestAsync(request);
// await client.SendAsync();
// }
// return null;
//}
}
}
......@@ -6,11 +6,11 @@ using System.Threading.Tasks;
namespace SendFileWebapi.Models
{
public class InputsDto
public class FilesPackage
{
public string ServerHost { get; set; }
public int Port { get; set; }
public IFormFileCollection FileNames { get; set; }
public IFormFileCollection UplodedFiles { get; set; }
public string ServerAET { 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