public Crawler Fetch() { if (!IsExternalIpAddress(this.SourceUrl)) { State = "INVALID_URL"; return(this); } var request = WebRequest.Create(this.SourceUrl) as HttpWebRequest; using (var response = request.GetResponse() as HttpWebResponse) { if (response != null && response.StatusCode != HttpStatusCode.OK) { State = "Url returns " + response.StatusCode + ", " + response.StatusDescription; return(this); } if (response != null && response.ContentType.IndexOf("image", StringComparison.Ordinal) == -1) { State = "Url is not an image"; return(this); } var sourceUri = new Uri(this.SourceUrl); ServerUrl = PathFormatter.Format(Path.GetFileName(sourceUri.AbsolutePath), Config.GetString("catcherPathFormat")); var savePath = Path.Combine(Config.WebRootPath, ServerUrl); //判断是否启用Oss存储 if (Config.GetBool("UseOss")) {//启用OSS存储 try { string extension = Path.GetExtension(Path.GetFileName(sourceUri.AbsolutePath)); var key = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(10000, 99999) + extension; ServerUrl = Config.GetString("OssViewBaseUrl") + key; OssApiHelper ossApiHelper = new OssApiHelper(); if (response != null) { var stream = response.GetResponseStream(); var result = ossApiHelper.PutObject(key, stream); if (result.Code == 0) { State = "SUCCESS"; } else { State = "Upload Oss Error:" + result.Message; } } else { State = "SUCCESS"; } } catch (Exception e) { State = "抓取错误:" + e.Message; } } else {//本地存储 if (!Directory.Exists(Path.GetDirectoryName(savePath))) { Directory.CreateDirectory(Path.GetDirectoryName(savePath)); } try { if (response != null) { var stream = response.GetResponseStream(); var reader = new BinaryReader(stream); byte[] bytes; using (var ms = new MemoryStream()) { byte[] buffer = new byte[4096]; int count; while ((count = reader.Read(buffer, 0, buffer.Length)) != 0) { ms.Write(buffer, 0, count); } bytes = ms.ToArray(); } File.WriteAllBytes(savePath, bytes); } State = "SUCCESS"; } catch (Exception e) { State = "抓取错误:" + e.Message; } } return(this); } }
public override UEditorResult Process() { byte[] uploadFileBytes = null; string uploadFileName = null; if (UploadConfig.Base64) { uploadFileName = UploadConfig.Base64Filename; uploadFileBytes = Convert.FromBase64String(Request.Form[UploadConfig.UploadFieldName]); } else { #if NETSTANDARD2_0 var file = Request.Form.Files[UploadConfig.UploadFieldName]; #endif #if NET35 var file = Request.Files[UploadConfig.UploadFieldName]; #endif uploadFileName = file.FileName; if (!CheckFileType(uploadFileName)) { Result.State = UploadState.TypeNotAllow; return(WriteResult()); } #if NETSTANDARD2_0 if (!CheckFileSize(file.Length)) #endif #if NET35 if (!CheckFileSize(file.ContentLength)) #endif { Result.State = UploadState.SizeLimitExceed; return(WriteResult()); } #if NETSTANDARD2_0 uploadFileBytes = new byte[file.Length]; try { file.OpenReadStream().Read(uploadFileBytes, 0, (int)file.Length); } #endif #if NET35 uploadFileBytes = new byte[file.ContentLength]; try { file.InputStream.Read(uploadFileBytes, 0, file.ContentLength); } #endif catch (Exception) { Result.State = UploadState.NetworkError; WriteResult(); } } Result.OriginFileName = uploadFileName; UEditorResult result; if (Config.GetBool("UseOss")) {//使用OSS存储 2019-10-16 try { string extension = Path.GetExtension(Path.GetFileName(uploadFileName)); var key = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(10000, 99999) + extension; Result.Url = Config.GetString("OssViewBaseUrl") + key; OssApiHelper ossApiHelper = new OssApiHelper(); using (Stream streams = new MemoryStream(uploadFileBytes)) { streams.Seek(0, SeekOrigin.Begin); var result1 = ossApiHelper.PutObject(key, streams); if (result1.Code == 0) { Result.State = UploadState.Success; } else { Result.State = UploadState.FileAccessError; Result.ErrorMessage = "Upload Oss Error:" + result1.Message; } } } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } finally { result = WriteResult(); } } else { var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat); var localPath = Path.Combine(Config.WebRootPath, savePath); try { if (!Directory.Exists(Path.GetDirectoryName(localPath))) { Directory.CreateDirectory(Path.GetDirectoryName(localPath)); } File.WriteAllBytes(localPath, uploadFileBytes); Result.Url = savePath; Result.State = UploadState.Success; } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } finally { result = WriteResult(); } } return(result); }