using HotelPms.Data;
|
using Google.Protobuf;
|
using Grpc.Core;
|
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Logging;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Threading.Tasks;
|
|
namespace HotelPms.GrpcService
|
{
|
public class FileService : FileCore.FileCoreBase
|
{
|
private readonly ILogger<FileService> _logger;
|
private IConfiguration m_Configuration;
|
|
public FileService(ILogger<FileService> logger, IConfiguration configuration)
|
{
|
_logger = logger;
|
m_Configuration = configuration;
|
}
|
|
public async override Task DownLoad(FileGrpcHead request, IServerStreamWriter<FileGrpcData> responseStream, ServerCallContext context)
|
{
|
//1M単位送信する
|
string path = System.AppDomain.CurrentDomain.BaseDirectory + @"DownLoad\" + request.FileName;
|
if (System.IO.File.Exists(path))
|
{
|
System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
|
long fileLength = fileInfo.Length;
|
|
using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
|
{
|
using (System.IO.BinaryReader br = new System.IO.BinaryReader(fs))
|
{
|
long offsetFile = 0;
|
long buffSize = 1024 * 1024; //1M
|
long lastBiteSize = fileLength % buffSize; //余り
|
long currentTimes = 1;
|
long loopTimes = fileLength / buffSize; //回数
|
if (lastBiteSize > 0) { loopTimes++; }
|
long blockSize = 0;
|
FileGrpcData contentStruct;
|
while (currentTimes <= loopTimes)
|
{
|
fs.Seek(offsetFile, System.IO.SeekOrigin.Begin);
|
if (currentTimes == loopTimes)
|
{
|
blockSize = lastBiteSize;
|
}
|
else
|
{
|
blockSize = buffSize;
|
}
|
|
byte[] byteArray = br.ReadBytes((int)blockSize);
|
ByteString sbytes = ByteString.CopyFrom(byteArray);
|
contentStruct = new FileGrpcData
|
{
|
FileName = request.FileName,
|
Block = (int)currentTimes,
|
Content = sbytes,
|
};
|
await responseStream.WriteAsync(contentStruct);
|
currentTimes++;
|
offsetFile += blockSize;
|
}
|
}
|
}
|
}
|
}
|
|
public override Task<FileGrpcHead> UpLoad(IAsyncStreamReader<FileGrpcData> requestStream, ServerCallContext context)
|
{
|
return base.UpLoad(requestStream, context);
|
}
|
}
|
}
|