1- using System ;
2- using System . IO ;
3- using System . Web ;
1+ using System . IO ;
42using SiteServer . CMS . Core ;
5- using SiteServer . CMS . Core . Office ;
6- using SiteServer . CMS . Plugin ;
73using SiteServer . Utils ;
84
95namespace SiteServer . BackgroundPages . Cms
106{
11- public class ModalTextEditorInsertImageHandler : IHttpHandler
7+ public class ModalTextEditorInsertImageHandler : BaseHandler
128 {
139 public static string GetRedirectUrl ( int siteId )
1410 {
1511 return PageUtils . GetCmsWebHandlerUrl ( siteId , nameof ( ModalTextEditorInsertImageHandler ) , null ) ;
1612 }
1713
18- public void ProcessRequest ( HttpContext context )
14+ protected override object Process ( )
1915 {
20- var body = new AuthRequest ( ) ;
16+ var fileName = AuthRequest . HttpRequest [ "fileName" ] ;
2117
22- if ( ! body . IsAdminLoggin ) return ;
23-
24- var request = context . Request ;
25-
26- var action = request [ "action" ] ;
27- var hash = request [ "hash" ] ;
28- var fileName = request [ "fileName" ] ;
29-
30- var fileCount = request . Files . Count ;
18+ var fileCount = AuthRequest . HttpRequest . Files . Count ;
3119
3220 string filePath = null ;
3321 string errorMessage = null ;
3422
35- var siteInfo = SiteManager . GetSiteInfo ( body . SiteId ) ;
23+ var siteInfo = SiteManager . GetSiteInfo ( AuthRequest . SiteId ) ;
3624
37- if ( string . IsNullOrEmpty ( hash ) )
25+ if ( fileCount > 0 )
3826 {
39- //普通上传
40- if ( fileCount > 0 )
41- {
42- var file = request . Files [ 0 ] ;
43-
44- //var fileName = Path.GetFileName(file.FileName);
45- //var path = context.Server.MapPath("~/upload/" + fileName);
27+ var file = AuthRequest . HttpRequest . Files [ 0 ] ;
4628
47- if ( string . IsNullOrEmpty ( fileName ) ) fileName = Path . GetFileName ( file . FileName ) ;
29+ //var fileName = Path.GetFileName(file.FileName);
30+ //var path = context.Server.MapPath("~/upload/" + fileName);
4831
49- filePath = file . FileName ;
50- var fileExtName = PathUtils . GetExtension ( filePath ) . ToLower ( ) ;
32+ if ( string . IsNullOrEmpty ( fileName ) ) fileName = Path . GetFileName ( file . FileName ) ;
5133
52- if ( ! PathUtility . IsImageExtenstionAllowed ( siteInfo , fileExtName ) )
53- {
54- errorMessage = "上传失败,上传图片格式不正确!" ;
55- }
56- if ( ! PathUtility . IsImageSizeAllowed ( siteInfo , file . ContentLength ) )
57- {
58- errorMessage = "上传失败,上传图片超出规定文件大小!" ;
59- }
34+ filePath = file . FileName ;
35+ var fileExtName = PathUtils . GetExtension ( filePath ) . ToLower ( ) ;
6036
61- if ( string . IsNullOrEmpty ( errorMessage ) )
62- {
63- var localDirectoryPath = PathUtility . GetUploadDirectoryPath ( siteInfo , fileExtName ) ;
64- var localFileName = PathUtility . GetUploadFileName ( siteInfo , filePath ) ;
65- filePath = PathUtils . Combine ( localDirectoryPath , localFileName ) ;
66- file . SaveAs ( filePath ) ;
67- }
68- }
69- }
70- else
71- {
72- //秒传或断点续传
73- //var path = context.Server.MapPath("~/upload/" + hash);
74- var path = PathUtils . GetTemporaryFilesPath ( hash ) ;
75-
76- var fileExtName = PathUtils . GetExtension ( fileName ) . ToLower ( ) ;
77- var localDirectoryPath = PathUtility . GetUploadDirectoryPath ( siteInfo , fileExtName ) ;
78- var localFileName = PathUtility . GetUploadFileName ( siteInfo , localDirectoryPath ) ;
79- var pathOk = PathUtils . Combine ( localDirectoryPath , localFileName ) ;
80-
81- //状态查询
82- if ( action == "query" )
37+ if ( ! PathUtility . IsImageExtenstionAllowed ( siteInfo , fileExtName ) )
8338 {
84- if ( File . Exists ( pathOk ) )
85- {
86- Finish ( GetResponseJson ( fileName , pathOk , string . Empty ) ) ;
87- }
88- else if ( File . Exists ( path ) )
89- {
90- Finish ( new FileInfo ( path ) . Length . ToString ( ) ) ;
91- }
92- else
93- {
94- Finish ( "0" ) ;
95- }
39+ errorMessage = "上传失败,上传图片格式不正确!" ;
9640 }
97- else
41+ if ( ! PathUtility . IsImageSizeAllowed ( siteInfo , file . ContentLength ) )
9842 {
99- if ( fileCount > 0 )
100- {
101- var file = request . Files [ 0 ] ;
102- using ( var fs = File . Open ( path , FileMode . Append ) )
103- {
104- byte [ ] buffer = new byte [ file . ContentLength ] ;
105- file . InputStream . Read ( buffer , 0 , file . ContentLength ) ;
106-
107- fs . Write ( buffer , 0 , buffer . Length ) ;
108- }
109- }
110-
111- var isOk = request [ "ok" ] == "1" ;
112- if ( ! isOk ) Finish ( "1" ) ;
43+ errorMessage = "上传失败,上传图片超出规定文件大小!" ;
44+ }
11345
114- if ( File . Exists ( path ) ) File . Move ( path , pathOk ) ;
46+ if ( string . IsNullOrEmpty ( errorMessage ) )
47+ {
48+ var localDirectoryPath = PathUtility . GetUploadDirectoryPath ( siteInfo , fileExtName ) ;
49+ var localFileName = PathUtility . GetUploadFileName ( siteInfo , filePath ) ;
50+ filePath = PathUtils . Combine ( localDirectoryPath , localFileName ) ;
51+ file . SaveAs ( filePath ) ;
11552 }
11653 }
11754
118- Finish ( GetResponseJson ( fileName , filePath , errorMessage ) ) ;
55+ return GetResponseObject ( fileName , filePath , errorMessage ) ;
11956 }
12057
12158 /// <summary>
12259 /// 获取返回的json字符串
12360 /// </summary>
12461 /// <returns></returns>
125- private static string GetResponseJson ( string fileName , string filePath , string errorMessage )
62+ private static object GetResponseObject ( string fileName , string filePath , string errorMessage )
12663 {
12764 FileInfo file = null ;
12865 if ( ! string . IsNullOrEmpty ( filePath ) )
@@ -131,34 +68,20 @@ private static string GetResponseJson(string fileName, string filePath, string e
13168 }
13269 if ( file != null )
13370 {
134- return TranslateUtils . JsonSerialize ( new
71+ return new
13572 {
13673 fileName ,
13774 filePath ,
13875 length = file ? . Length ,
13976 ret = 1
140- } ) ;
77+ } ;
14178 }
14279
143- return TranslateUtils . JsonSerialize ( new
80+ return new
14481 {
14582 ret = 0 ,
14683 errorMessage
147- } ) ;
148- }
149-
150- /// <summary>
151- /// 完成上传
152- /// </summary>
153- /// <param name="json">回调函数参数</param>
154- private static void Finish ( string json )
155- {
156- var response = HttpContext . Current . Response ;
157-
158- response . Write ( json ) ;
159- response . End ( ) ;
84+ } ;
16085 }
161-
162- public bool IsReusable => false ;
16386 }
16487}
0 commit comments