Beispiel #1
0
    public Crawler Fetch()
    {
        if (!IsExternalIPAddress(this.SourceUrl))
        {
            State = "INVALID_URL";
            return(this);
        }
        var request = HttpWebRequest.Create(this.SourceUrl) as HttpWebRequest;

        using (var response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
            {
                State = "Url returns " + response.StatusCode + ", " + response.StatusDescription;
                return(this);
            }
            if (response.ContentType.IndexOf("image") == -1)
            {
                State = "Url is not an image";
                return(this);
            }
            ServerUrl = PathFormatter.Format(Path.GetFileName(this.SourceUrl), MConfig.GetString("catcherPathFormat"));
            var savePath = Server.MapPath(ServerUrl);
            if (!Directory.Exists(Path.GetDirectoryName(savePath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(savePath));
            }
            try
            {
                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 void ProcessRequest(HttpContext context)
        {
            IUser user = UserContext.CurrentUser;

            if (user == null)
            {
                context.Response.Redirect(SiteUrls.Instance().Login());
            }
            MHandler action = null;

            switch (context.Request["action"])
            {
            case "config":
                action = new MConfigHandler(context);
                break;

            case "uploadimage":
                action = new MUploadHandler(context, new MUploadConfig()
                {
                    MAllowExtensions = MConfig.GetStringList("imageAllowFiles"),
                    MPathFormat      = MConfig.GetString("imagePathFormat"),
                    MSizeLimit       = MConfig.GetInt("imageMaxSize"),
                    MUploadFieldName = MConfig.GetString("imageFieldName")
                });
                break;

            case "uploadscrawl":
                action = new MUploadHandler(context, new MUploadConfig()
                {
                    MAllowExtensions = new string[] { ".png" },
                    MPathFormat      = MConfig.GetString("scrawlPathFormat"),
                    MSizeLimit       = MConfig.GetInt("scrawlMaxSize"),
                    MUploadFieldName = MConfig.GetString("scrawlFieldName"),
                    MBase64          = true,
                    MBase64Filename  = "scrawl.png"
                });
                break;

            case "uploadvideo":
                action = new MUploadHandler(context, new MUploadConfig()
                {
                    MAllowExtensions = MConfig.GetStringList("videoAllowFiles"),
                    MPathFormat      = MConfig.GetString("videoPathFormat"),
                    MSizeLimit       = MConfig.GetInt("videoMaxSize"),
                    MUploadFieldName = MConfig.GetString("videoFieldName")
                });
                break;

            case "uploadfile":
                action = new MUploadHandler(context, new MUploadConfig()
                {
                    MAllowExtensions = MConfig.GetStringList("fileAllowFiles"),
                    MPathFormat      = MConfig.GetString("filePathFormat"),
                    MSizeLimit       = MConfig.GetInt("fileMaxSize"),
                    MUploadFieldName = MConfig.GetString("fileFieldName")
                });
                break;

            case "listimage":
                action = new MListFileManager(context, MConfig.GetString("imageManagerListPath"), MConfig.GetStringList("imageManagerAllowFiles"));
                break;

            case "listfile":
                action = new MListFileManager(context, MConfig.GetString("fileManagerListPath"), MConfig.GetStringList("fileManagerAllowFiles"));
                break;

            case "catchimage":
                action = new MCrawlerHandler(context);
                break;

            default:
                action = new MNotSupportedHandler(context);
                break;
            }
            action.Process();
        }