Ejemplo n.º 1
0
        public Crawler Fetch()
        {
            if (!SourceUrl.IsExternalAddress() || SourceUrl.Contains(AppConfig.ImgbedDomains))
            {
                State = "INVALID_URL";
                return(this);
            }
            var request = WebRequest.Create(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(SourceUrl), UeditorConfig.GetString("catcherPathFormat"));

                try
                {
                    using (var stream = response.GetResponseStream())
                    {
                        var savePath = AppContext.BaseDirectory + "wwwroot" + ServerUrl;
                        using (var httpClient = new HttpClient())
                        {
                            var(url, success) = new ImagebedClient(httpClient).UploadImage(stream, savePath).Result;
                            if (success)
                            {
                                BackgroundJob.Enqueue(() => File.Delete(savePath));
                                ServerUrl = url;
                            }
                            else
                            {
                                if (!Directory.Exists(Path.GetDirectoryName(savePath)))
                                {
                                    Directory.CreateDirectory(Path.GetDirectoryName(savePath));
                                }
                                using (var ms = new MemoryStream())
                                {
                                    stream.CopyTo(ms);
                                    File.WriteAllBytes(savePath, ms.GetBuffer());
                                }
                            }
                        }
                        State = "SUCCESS";
                    }
                }
                catch (Exception e)
                {
                    State = "抓取错误:" + e.Message;
                }
                return(this);
            }
        }
        public override void Parse()
        {
            SourceUrl            = Args[0];
            ExternalDependencies = ParseParameterSwitch("-x");
            ZipDependencies      = ParseParameterSwitch("-z");
            DisplayHtml          = ParseParameterSwitch("-d");
            TargetFile           = ParseStringParameterSwitch("-o", null);

            if (string.IsNullOrEmpty(TargetFile))
            {
                ExternalDependencies = false;
            }

            if (SourceUrl.IndexOf("http", StringComparison.InvariantCultureIgnoreCase) == -1 && SourceUrl.Contains("%"))
            {
                SourceUrl = Environment.ExpandEnvironmentVariables(SourceUrl);
            }

            if (!string.IsNullOrEmpty(TargetFile) && TargetFile.Contains("%"))
            {
                TargetFile = Environment.ExpandEnvironmentVariables(TargetFile);
            }
        }