markus / FileUploadWevService / FileUpload.asmx.cs @ 66bd3240
이력 | 보기 | 이력해설 | 다운로드 (10.9 KB)
1 |
using KCOMDataModel.DataModel; |
---|---|
2 |
using SvgNet; |
3 |
using SvgNet.Elements; |
4 |
using SvgNet.Interfaces; |
5 |
using System; |
6 |
using System.Collections.Generic; |
7 |
using System.Configuration; |
8 |
using System.Drawing; |
9 |
using System.Drawing.Drawing2D; |
10 |
using System.IO; |
11 |
using System.Linq; |
12 |
using System.Text; |
13 |
using System.Web; |
14 |
using System.Web.Services; |
15 |
|
16 |
namespace FileUploadWevService |
17 |
{ |
18 |
/// <summary> |
19 |
/// FileUpload의 요약 설명입니다. |
20 |
/// </summary> |
21 |
[WebService(Namespace = "http://tempuri.org/")] |
22 |
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] |
23 |
[System.ComponentModel.ToolboxItem(false)] |
24 |
// ASP.NET AJAX를 사용하여 스크립트에서 이 웹 서비스를 호출하려면 다음 줄의 주석 처리를 제거합니다. |
25 |
// [System.Web.Script.Services.ScriptService] |
26 |
public class FileUpload : System.Web.Services.WebService |
27 |
{ |
28 |
|
29 |
[WebMethod] |
30 |
//public string Run(string ProjectNo, string VPNo, string UserID, string FileName, byte[] imageBytes) |
31 |
public string Run(string ProjectNo, string VPNo, string UserID, string FileName, byte[] f) |
32 |
{ |
33 |
string sConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; |
34 |
KCOMEntities entity = new KCOMEntities(sConnString); |
35 |
|
36 |
/// 사용자가 Comment한 이미지 URL를 생성한다. |
37 |
|
38 |
var item = entity.PROPERTIES.Where(data => data.TYPE == "SystemInfo" && data.PROPERTY == "Url").FirstOrDefault(); |
39 |
string result = ""; |
40 |
if (item != null) |
41 |
{ |
42 |
result = string.Format(@"{0}/UserData/{1}/{2}/{3}", item.VALUE, ProjectNo, UserID, FileName); |
43 |
} |
44 |
else |
45 |
{ |
46 |
throw new Exception("SystemInfo 정보가 없습니다."); |
47 |
} |
48 |
|
49 |
try |
50 |
{ |
51 |
var TileSourceProperty = entity.PROPERTIES.Where(data => data.TYPE == "TileSorceStorage" && data.PROPERTY == ProjectNo).FirstOrDefault(); |
52 |
if (TileSourceProperty == null) |
53 |
{ |
54 |
throw new Exception("TileSourceStorage 정보가 없습니다."); |
55 |
} |
56 |
/// 실제 이미지를 저장할 위치 |
57 |
string DirectoryPath = string.Format(Path.Combine(TileSourceProperty.VALUE, "UserData", ProjectNo, UserID)); |
58 |
DirectoryInfo directoryInfo_ = new DirectoryInfo(DirectoryPath); |
59 |
|
60 |
if (!directoryInfo_.Exists) |
61 |
{ |
62 |
directoryInfo_.Create(); |
63 |
} |
64 |
//Image imageinfo = Converter(base64String); |
65 |
|
66 |
MemoryStream ms = new MemoryStream(f); |
67 |
FileStream fs = new FileStream(directoryInfo_ + @"\" + FileName, FileMode.Create); |
68 |
ms.WriteTo(fs); |
69 |
|
70 |
ms.Close(); |
71 |
fs.Close(); |
72 |
fs.Dispose(); |
73 |
//imageinfo.Save(DirectoryPath + @"\" + FileName + "png", System.Drawing.Imaging.ImageFormat.Png); |
74 |
} |
75 |
catch (Exception ex) |
76 |
{ |
77 |
result = ex.ToString(); |
78 |
} |
79 |
|
80 |
return result; |
81 |
} |
82 |
|
83 |
[WebMethod] |
84 |
//public string Run(string ProjectNo, string VPNo, string UserID, string FileName, byte[] imageBytes) |
85 |
public string RunSymbol(string ProjectNo, string VPNo, string UserID, string FileName, byte[] f) |
86 |
{ |
87 |
string sConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; |
88 |
KCOMEntities entity = new KCOMEntities(sConnString); |
89 |
|
90 |
/// 사용자가 Comment한 이미지 URL를 생성한다. |
91 |
|
92 |
var item = entity.PROPERTIES.Where(data => data.TYPE == "SystemInfo" && data.PROPERTY == "Url").FirstOrDefault(); |
93 |
string result = ""; |
94 |
if (item != null) |
95 |
{ |
96 |
FileName = FileName.Replace(".png", ".svg"); |
97 |
|
98 |
result = string.Format(@"{0}/UserData/{1}/{2}/{3}", item.VALUE, ProjectNo, UserID, FileName); |
99 |
} |
100 |
else |
101 |
{ |
102 |
throw new Exception("SystemInfo 정보가 없습니다."); |
103 |
} |
104 |
|
105 |
try |
106 |
{ |
107 |
var TileSourceProperty = entity.PROPERTIES.Where(data => data.TYPE == "TileSorceStorage" && data.PROPERTY == ProjectNo).FirstOrDefault(); |
108 |
if (TileSourceProperty == null) |
109 |
{ |
110 |
throw new Exception("TileSourceStorage 정보가 없습니다."); |
111 |
} |
112 |
/// 실제 이미지를 저장할 위치 |
113 |
string DirectoryPath = string.Format(Path.Combine(TileSourceProperty.VALUE, "UserData", ProjectNo, UserID)); |
114 |
DirectoryInfo directoryInfo_ = new DirectoryInfo(DirectoryPath); |
115 |
|
116 |
if (!directoryInfo_.Exists) |
117 |
{ |
118 |
directoryInfo_.Create(); |
119 |
} |
120 |
|
121 |
using (MemoryStream ms = new MemoryStream(f)) |
122 |
{ |
123 |
using (var image = Image.FromStream(ms)) |
124 |
{ |
125 |
using (var ig = new SvgGraphics(Color.Transparent)) |
126 |
{ |
127 |
ig.PixelOffsetMode = PixelOffsetMode.HighSpeed; |
128 |
ig.DrawImage(image, 0, 0); |
129 |
GraphicsContainer cnt = ig.BeginContainer(); |
130 |
ig.EndContainer(cnt); |
131 |
|
132 |
string s = ig.WriteSVGString(); |
133 |
string tempFile = Path.Combine(directoryInfo_.FullName, FileName); |
134 |
var tw = new StreamWriter(tempFile, false); |
135 |
tw.Write(s); |
136 |
tw.Close(); |
137 |
tw.Dispose(); |
138 |
ig.Dispose(); |
139 |
} |
140 |
} |
141 |
} |
142 |
} |
143 |
catch (Exception ex) |
144 |
{ |
145 |
result = ex.ToString(); |
146 |
} |
147 |
|
148 |
return result; |
149 |
} |
150 |
|
151 |
|
152 |
|
153 |
[WebMethod] |
154 |
public string ServerMapPathTest(string uri) |
155 |
{ |
156 |
return GetFilePath(uri); |
157 |
} |
158 |
|
159 |
private string GetFilePath(string fileUri) |
160 |
{ |
161 |
string filePath = null; |
162 |
|
163 |
try |
164 |
{ |
165 |
string sConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; |
166 |
KCOMEntities entity = new KCOMEntities(sConnString); |
167 |
|
168 |
var sysinfo = entity.PROPERTIES.Where(data => data.TYPE == "SystemInfo" && data.PROPERTY == "Url").FirstOrDefault(); |
169 |
|
170 |
filePath = HttpContext.Current.Server.MapPath(new Uri(fileUri.Replace(sysinfo.VALUE, $"{ "Http://" + HttpContext.Current.Request.Url.Authority}")).LocalPath); |
171 |
} |
172 |
catch (Exception ex) |
173 |
{ |
174 |
throw; |
175 |
} |
176 |
|
177 |
return filePath; |
178 |
} |
179 |
|
180 |
|
181 |
[WebMethod] |
182 |
public string SymbolRegen() |
183 |
{ |
184 |
string result = ""; |
185 |
|
186 |
try |
187 |
{ |
188 |
StringBuilder sb = new StringBuilder(); |
189 |
|
190 |
string sConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; |
191 |
KCOMEntities entity = new KCOMEntities(sConnString); |
192 |
|
193 |
var sysinfo = entity.PROPERTIES.Where(data => data.TYPE == "SystemInfo" && data.PROPERTY == "Url").FirstOrDefault(); |
194 |
|
195 |
foreach (var symbol in entity.SYMBOL_PRIVATE.Where(x => !x.IMAGE_URL.EndsWith(".svg"))) |
196 |
{ |
197 |
//if (symbol.IMAGE_URL.StartsWith(sysinfo.VALUE)) |
198 |
//{ |
199 |
// symbol.IMAGE_URL = symbol.IMAGE_URL.Replace(sysinfo.VALUE, ""); |
200 |
//} |
201 |
|
202 |
if (symbol.IMAGE_URL.EndsWith(".png") || symbol.IMAGE_URL.EndsWith(".jpg")) |
203 |
{ |
204 |
var filePath = GetFilePath(symbol.IMAGE_URL); |
205 |
|
206 |
symbol.IMAGE_URL = symbol.IMAGE_URL.Replace(".png", ".svg").Replace(".jpg", ".svg"); |
207 |
|
208 |
using (var image = Image.FromFile(filePath)) |
209 |
{ |
210 |
using (var ig = new SvgGraphics(Color.Transparent)) |
211 |
{ |
212 |
ig.SmoothingMode = SmoothingMode.HighSpeed; |
213 |
ig.DrawImage(image, 0, 0); |
214 |
GraphicsContainer cnt = ig.BeginContainer(); |
215 |
ig.EndContainer(cnt); |
216 |
|
217 |
string s = ig.WriteSVGString(); |
218 |
string svgFile = filePath.Replace(".png", ".svg").Replace(".jpg", ".svg"); |
219 |
|
220 |
var tw = new StreamWriter(svgFile, false); |
221 |
tw.Write(s); |
222 |
tw.Close(); |
223 |
tw.Dispose(); |
224 |
ig.Dispose(); |
225 |
} |
226 |
} |
227 |
} |
228 |
} |
229 |
|
230 |
foreach (var symbol in entity.SYMBOL_PUBLIC.Where(x => !x.IMAGE_URL.EndsWith(".svg"))) |
231 |
{ |
232 |
//if (symbol.IMAGE_URL.StartsWith(sysinfo.VALUE)) |
233 |
//{ |
234 |
// symbol.IMAGE_URL = symbol.IMAGE_URL.Replace(sysinfo.VALUE, ""); |
235 |
//} |
236 |
|
237 |
if (symbol.IMAGE_URL.EndsWith(".png") || symbol.IMAGE_URL.EndsWith(".jpg")) |
238 |
{ |
239 |
var filePath = GetFilePath(symbol.IMAGE_URL); |
240 |
|
241 |
symbol.IMAGE_URL = symbol.IMAGE_URL.Replace(".png", ".svg").Replace(".jpg", ".svg"); |
242 |
|
243 |
using (var image = Image.FromFile(filePath)) |
244 |
{ |
245 |
using (var ig = new SvgGraphics(Color.Transparent)) |
246 |
{ |
247 |
ig.DrawImage(image, 0, 0); |
248 |
GraphicsContainer cnt = ig.BeginContainer(); |
249 |
ig.EndContainer(cnt); |
250 |
|
251 |
string s = ig.WriteSVGString(); |
252 |
string svgFile = filePath.Replace(".png", ".svg").Replace(".jpg", ".svg"); |
253 |
|
254 |
var tw = new StreamWriter(svgFile, false); |
255 |
tw.Write(s); |
256 |
tw.Close(); |
257 |
tw.Dispose(); |
258 |
ig.Dispose(); |
259 |
} |
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
entity.SaveChanges(); |
265 |
|
266 |
result = "OK"; |
267 |
} |
268 |
catch (Exception) |
269 |
{ |
270 |
throw; |
271 |
} |
272 |
return result; |
273 |
} |
274 |
|
275 |
|
276 |
public Image Converter(string base64String) |
277 |
{ |
278 |
byte[] imageBytes = Convert.FromBase64String(base64String); |
279 |
|
280 |
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) |
281 |
{ |
282 |
Image image = Image.FromStream(ms, true); |
283 |
return image; |
284 |
} |
285 |
} |
286 |
} |
287 |
} |