ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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);
        }
    }
}