프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / License.DB / Database.cs @ cf1cc862

이력 | 보기 | 이력해설 | 다운로드 (6.59 KB)

1
using LiteDB;
2
using System;
3
using System.Collections.Generic;
4
using System.IO;
5
using System.Linq;
6
using System.Net;
7
using System.Runtime.Serialization;
8
using System.Runtime.Serialization.Json;
9
using System.Text;
10
using System.Threading.Tasks;
11

    
12
namespace License.DB
13
{
14
    public class DataBase : iDatabase
15
    {
16
        const int initial = 10 * 8192;
17
        private readonly string ConnectionString;
18

    
19
        private readonly string DataBaseFoloder;
20
        private readonly string DataBaseFile;
21

    
22
        private readonly string LogConnectionString;
23
        private readonly string LogDataBaseFile;
24

    
25

    
26
        public DataBase()
27
        {
28
            DataBaseFoloder = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DataBase\\Active");
29

    
30
            if (!System.IO.Directory.Exists(DataBaseFoloder))
31
            {
32
                System.IO.Directory.CreateDirectory(DataBaseFoloder);
33
            }
34

    
35
            DataBaseFile = System.IO.Path.Combine(DataBaseFoloder, $"{DateTime.Today.ToString("yyyyMMdd")}.db");
36

    
37
            ConnectionString = $"Filename={DataBaseFile};Password=dof1073#;Connection=shared";
38

    
39
            LogDataBaseFile = System.IO.Path.Combine(DataBaseFoloder, "Log.db");
40

    
41
            LogConnectionString = $"Filename={LogDataBaseFile};Password=dof1073#;Connection=shared";
42
        }
43

    
44
        public void Active(string MachineName, string Process, string UserName, string license)
45
        {
46
            using (var db = new LiteDatabase(ConnectionString))
47
            {
48
                var collection = db.GetCollection<Active>("active");
49

    
50
                var items = collection.Query().Where(x => x.Process == Process && x.MachineName == MachineName && x.UserName == UserName).FirstOrDefault();
51

    
52
                System.Diagnostics.Debug.WriteLine("item : " + items?.UserName);
53

    
54
                System.Diagnostics.Debug.WriteLine(MachineName + " " + Process + "  " + UserName);
55

    
56
                if (items == null)
57
                {
58
                    collection.Insert(new DB.Active(UserName, MachineName, Process, DateTime.Now, DateTime.MinValue));
59
                }
60
                else
61
                {
62
                    items.UpdateTime = DateTime.Now;
63
                    collection.Update(items);
64
                }
65
            }
66
        }
67

    
68
        public async Task<bool> UploadAsync(Uri uri,string SiteName)
69
        {
70
            bool result = false;
71

    
72
            try
73
            {
74
                var databasedir = new DirectoryInfo(DataBaseFoloder);
75

    
76
                if(databasedir.EnumerateFiles().Count() > 0)
77
                {
78
                    var items = databasedir.GetFiles().Where(x=>x.Name != $"{DateTime.Today.ToString("yyyyMMdd")}.db")
79
                                            .Select(x => new { key = int.Parse(x.Name.Replace(".db", "")), fileName = x.Name})
80
                                            .OrderByDescending(x => x.key);
81

    
82
                    foreach (var item in items)
83
                    {
84
                        var fileSize = await FileSizeAsync(uri, SiteName, item.fileName);
85

    
86
                        if(int.Parse(fileSize) < 0)
87
                        {
88
                            var filepath = System.IO.Path.Combine(DataBaseFoloder, item.fileName);
89
                            var uploadFilename = await UploadFileAsync(uri, SiteName, filepath);
90
                        }
91
                        else
92
                        {
93
                            break;
94
                        }
95
                    }
96

    
97
                    //using (var db = new LiteDatabase(LogConnectionString))
98
                    //{
99
                    //    var collection = db.GetCollection<UploadItem>("Upload");
100

    
101
                    //    var items = collection.Query().Where(x => x.FileName == filename).FirstOrDefault();
102

    
103
                    //    if (items == null && File.Exists(filepath))
104
                    //    {
105
                    //        var uploadFilename = await UploadFileAsync(uri,SiteName, filepath);
106
                    //        var size = await FileSizeAsync(uri, filename);
107

    
108
                    //        var localfileLength = new System.IO.FileInfo(filepath).Length;
109

    
110
                    //        if (long.Parse(size) == localfileLength)
111
                    //        {
112
                    //            collection.Insert(new UploadItem(filename, localfileLength));
113
                    //            result = true;
114
                    //        }
115
                    //    }
116
                    //    else
117
                    //    {
118
                    //        result = true;
119
                    //    }
120
                    //}
121
                }
122
            }
123
            catch (Exception)
124
            {
125
                throw;
126
            }
127

    
128
            return result;
129
        }
130

    
131
        private async Task<string> UploadFileAsync(Uri _url,string SiteName, string filePath)
132
        {
133
            try
134
            {
135
                ServicePointManager.Expect100Continue = true;
136
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
137

    
138
                WebClient client = new WebClient();
139
                client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
140
                client.Headers.Add("Cache-Control", "no-cache");
141
                client.Encoding = Encoding.UTF8;
142

    
143
                var result = await client.UploadFileTaskAsync($"{_url}/Site={SiteName}", filePath);
144
                string reply = System.Text.Encoding.UTF8.GetString(result);
145

    
146
                return reply;
147
            }
148
            catch (Exception)
149
            {
150
                throw;
151
            }
152
        }
153

    
154
        private async Task<string> FileSizeAsync(Uri _url, string SiteName, string filePath)
155
        {
156
            try
157
            {
158
                ServicePointManager.Expect100Continue = true;
159
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
160

    
161
                WebClient client = new WebClient();
162
                client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
163
                client.Headers.Add("Cache-Control", "no-cache");
164
                client.Encoding = Encoding.UTF8;
165

    
166
                var data = await client.DownloadStringTaskAsync($"{_url}/FileInfo/Site={SiteName}&file={filePath}");
167
                return data;
168
            }
169
            catch (Exception)
170
            {
171
                throw;
172
            }
173
        }
174

    
175
        private async Task<string> FileListAsync(Uri _url, string SiteName)
176
        {
177
            try
178
            {
179
                return "";
180
            }
181
            catch (Exception)
182
            {
183
                throw;
184
            }
185
        }
186
    }
187
}
클립보드 이미지 추가 (최대 크기: 500 MB)