markus / FileUploadWevService / FileUpload.asmx.cs @ 5639752b
이력 | 보기 | 이력해설 | 다운로드 (11.5 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 |
|
95 |
if (item == null) |
96 |
{ |
97 |
throw new Exception("SystemInfo 정보가 없습니다."); |
98 |
} |
99 |
|
100 |
try |
101 |
{ |
102 |
var TileSourceProperty = entity.PROPERTIES.Where(data => data.TYPE == "TileSorceStorage" && data.PROPERTY == ProjectNo).FirstOrDefault(); |
103 |
if (TileSourceProperty == null) |
104 |
{ |
105 |
throw new Exception("TileSourceStorage 정보가 없습니다."); |
106 |
} |
107 |
|
108 |
/// 실제 이미지를 저장할 위치 |
109 |
string DirectoryPath = string.Format(Path.Combine(TileSourceProperty.VALUE, "UserData", ProjectNo, UserID)); |
110 |
DirectoryInfo directoryInfo_ = new DirectoryInfo(DirectoryPath); |
111 |
|
112 |
if (!directoryInfo_.Exists) |
113 |
{ |
114 |
directoryInfo_.Create(); |
115 |
} |
116 |
|
117 |
if (!ImageUploadWebService.Properties.Settings.Default.IsSaveSVG) |
118 |
{ |
119 |
MemoryStream ms = new MemoryStream(f); |
120 |
FileStream fs = new FileStream(directoryInfo_ + @"\" + FileName, FileMode.Create); |
121 |
ms.WriteTo(fs); |
122 |
|
123 |
ms.Close(); |
124 |
fs.Close(); |
125 |
fs.Dispose(); |
126 |
} |
127 |
else |
128 |
{ |
129 |
FileName = FileName.Replace(".png", ".svg"); |
130 |
|
131 |
using (MemoryStream ms = new MemoryStream(f)) |
132 |
{ |
133 |
using (var image = Image.FromStream(ms)) |
134 |
{ |
135 |
using (var ig = new SvgGraphics(Color.Transparent)) |
136 |
{ |
137 |
//ig.PixelOffsetMode = PixelOffsetMode.HighSpeed; |
138 |
ig.CompositingQuality = CompositingQuality.HighSpeed; |
139 |
|
140 |
ig.DrawImage(image, 0, 0); |
141 |
GraphicsContainer cnt = ig.BeginContainer(); |
142 |
ig.EndContainer(cnt); |
143 |
|
144 |
string s = ig.WriteSVGString(); |
145 |
string tempFile = Path.Combine(directoryInfo_.FullName, FileName); |
146 |
var tw = new StreamWriter(tempFile, false); |
147 |
tw.Write(s); |
148 |
tw.Close(); |
149 |
tw.Dispose(); |
150 |
ig.Dispose(); |
151 |
} |
152 |
} |
153 |
} |
154 |
} |
155 |
|
156 |
result = string.Format(@"{0}/UserData/{1}/{2}/{3}", item.VALUE, ProjectNo, UserID, FileName); |
157 |
} |
158 |
catch (Exception ex) |
159 |
{ |
160 |
result = ex.ToString(); |
161 |
} |
162 |
|
163 |
return result; |
164 |
} |
165 |
|
166 |
|
167 |
|
168 |
[WebMethod] |
169 |
public string ServerMapPathTest(string uri) |
170 |
{ |
171 |
return GetFilePath(uri); |
172 |
} |
173 |
|
174 |
private string GetFilePath(string fileUri) |
175 |
{ |
176 |
string filePath = null; |
177 |
|
178 |
try |
179 |
{ |
180 |
string sConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; |
181 |
KCOMEntities entity = new KCOMEntities(sConnString); |
182 |
|
183 |
var sysinfo = entity.PROPERTIES.Where(data => data.TYPE == "SystemInfo" && data.PROPERTY == "Url").FirstOrDefault(); |
184 |
|
185 |
filePath = HttpContext.Current.Server.MapPath(new Uri(fileUri.Replace(sysinfo.VALUE, $"{ "Http://" + HttpContext.Current.Request.Url.Authority}")).LocalPath); |
186 |
} |
187 |
catch (Exception ex) |
188 |
{ |
189 |
throw; |
190 |
} |
191 |
|
192 |
return filePath; |
193 |
} |
194 |
|
195 |
|
196 |
[WebMethod] |
197 |
public string SymbolRegen() |
198 |
{ |
199 |
string result = ""; |
200 |
|
201 |
try |
202 |
{ |
203 |
StringBuilder sb = new StringBuilder(); |
204 |
|
205 |
string sConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; |
206 |
KCOMEntities entity = new KCOMEntities(sConnString); |
207 |
|
208 |
var sysinfo = entity.PROPERTIES.Where(data => data.TYPE == "SystemInfo" && data.PROPERTY == "Url").FirstOrDefault(); |
209 |
|
210 |
foreach (var symbol in entity.SYMBOL_PRIVATE.Where(x => !x.IMAGE_URL.EndsWith(".svg"))) |
211 |
{ |
212 |
//if (symbol.IMAGE_URL.StartsWith(sysinfo.VALUE)) |
213 |
//{ |
214 |
// symbol.IMAGE_URL = symbol.IMAGE_URL.Replace(sysinfo.VALUE, ""); |
215 |
//} |
216 |
|
217 |
if (symbol.IMAGE_URL.EndsWith(".png") || symbol.IMAGE_URL.EndsWith(".jpg")) |
218 |
{ |
219 |
var filePath = GetFilePath(symbol.IMAGE_URL); |
220 |
|
221 |
symbol.IMAGE_URL = symbol.IMAGE_URL.Replace(".png", ".svg").Replace(".jpg", ".svg"); |
222 |
|
223 |
using (var image = Image.FromFile(filePath)) |
224 |
{ |
225 |
using (var ig = new SvgGraphics(Color.Transparent)) |
226 |
{ |
227 |
ig.SmoothingMode = SmoothingMode.HighSpeed; |
228 |
ig.DrawImage(image, 0, 0); |
229 |
GraphicsContainer cnt = ig.BeginContainer(); |
230 |
ig.EndContainer(cnt); |
231 |
|
232 |
string s = ig.WriteSVGString(); |
233 |
string svgFile = filePath.Replace(".png", ".svg").Replace(".jpg", ".svg"); |
234 |
|
235 |
var tw = new StreamWriter(svgFile, false); |
236 |
tw.Write(s); |
237 |
tw.Close(); |
238 |
tw.Dispose(); |
239 |
ig.Dispose(); |
240 |
} |
241 |
} |
242 |
} |
243 |
} |
244 |
|
245 |
foreach (var symbol in entity.SYMBOL_PUBLIC.Where(x => !x.IMAGE_URL.EndsWith(".svg"))) |
246 |
{ |
247 |
//if (symbol.IMAGE_URL.StartsWith(sysinfo.VALUE)) |
248 |
//{ |
249 |
// symbol.IMAGE_URL = symbol.IMAGE_URL.Replace(sysinfo.VALUE, ""); |
250 |
//} |
251 |
|
252 |
if (symbol.IMAGE_URL.EndsWith(".png") || symbol.IMAGE_URL.EndsWith(".jpg")) |
253 |
{ |
254 |
var filePath = GetFilePath(symbol.IMAGE_URL); |
255 |
|
256 |
symbol.IMAGE_URL = symbol.IMAGE_URL.Replace(".png", ".svg").Replace(".jpg", ".svg"); |
257 |
|
258 |
using (var image = Image.FromFile(filePath)) |
259 |
{ |
260 |
using (var ig = new SvgGraphics(Color.Transparent)) |
261 |
{ |
262 |
ig.DrawImage(image, 0, 0); |
263 |
GraphicsContainer cnt = ig.BeginContainer(); |
264 |
ig.EndContainer(cnt); |
265 |
|
266 |
string s = ig.WriteSVGString(); |
267 |
string svgFile = filePath.Replace(".png", ".svg").Replace(".jpg", ".svg"); |
268 |
|
269 |
var tw = new StreamWriter(svgFile, false); |
270 |
tw.Write(s); |
271 |
tw.Close(); |
272 |
tw.Dispose(); |
273 |
ig.Dispose(); |
274 |
} |
275 |
} |
276 |
} |
277 |
} |
278 |
|
279 |
entity.SaveChanges(); |
280 |
|
281 |
result = "OK"; |
282 |
} |
283 |
catch (Exception) |
284 |
{ |
285 |
throw; |
286 |
} |
287 |
return result; |
288 |
} |
289 |
|
290 |
|
291 |
public Image Converter(string base64String) |
292 |
{ |
293 |
byte[] imageBytes = Convert.FromBase64String(base64String); |
294 |
|
295 |
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) |
296 |
{ |
297 |
Image image = Image.FromStream(ms, true); |
298 |
return image; |
299 |
} |
300 |
} |
301 |
} |
302 |
} |