Пример #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);
            }
        }
Пример #2
0
        public Crawler Fetch()
        {
            if (!SourceUrl.IsExternalAddress())
            {
                State = "INVALID_URL";
                return(this);
            }

            using (var httpClient = new HttpClient())
            {
                var response = httpClient.GetAsync(SourceUrl).Result;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    State = "Url returns " + response.StatusCode;
                    return(this);
                }

                ServerUrl = PathFormatter.Format(Path.GetFileName(SourceUrl), UeditorConfig.GetString("catcherPathFormat"));

                try
                {
                    using (var stream = response.Content.ReadAsStreamAsync().Result)
                    {
                        var savePath = AppContext.BaseDirectory + "wwwroot" + ServerUrl;
                        var(url, success) = new ImagebedClient(httpClient).UploadImage(stream, savePath).Result;
                        if (success)
                        {
                            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);
            }
        }
Пример #3
0
        public async Task <Crawler> Fetch()
        {
            if (!SourceUrl.IsExternalAddress())
            {
                State = "INVALID_URL";
                return(this);
            }
            try
            {
                using var response = _httpClient.GetAsync(SourceUrl).Result;
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    State = "Url returns " + response.StatusCode;
                    return(this);
                }

                ServerUrl = PathFormatter.Format(Path.GetFileName(SourceUrl), UeditorConfig.GetString("catcherPathFormat"));
                var stream   = response.Content.ReadAsStreamAsync().Result;
                var savePath = AppContext.BaseDirectory + "wwwroot" + ServerUrl;
                stream            = stream.AddWatermark();
                var(url, success) = Startup.ServiceProvider.GetRequiredService <ImagebedClient>().UploadImage(stream, savePath).Result;
                if (success)
                {
                    ServerUrl = url;
                }
                else
                {
                    if (!Directory.Exists(Path.GetDirectoryName(savePath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(savePath));
                    }

                    var ms = new MemoryStream();
                    stream.CopyTo(ms);
                    File.WriteAllBytes(savePath, ms.GetBuffer());
                }
                stream.Close();
                await stream.DisposeAsync();

                State = "SUCCESS";
            }
            catch (Exception e)
            {
                State = "抓取错误:" + e.Message;
                LogManager.Error(e);
            }

            return(this);
        }
Пример #4
0
        public async Task <Crawler> Fetch()
        {
            if (!SourceUrl.IsExternalAddress())
            {
                State = "INVALID_URL";
                return(this);
            }
            try
            {
                using var response = await _httpClient.GetAsync(SourceUrl);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    State = "Url returns " + response.StatusCode;
                    return(this);
                }

                ServerUrl = PathFormatter.Format(Path.GetFileName(SourceUrl), CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("catcherPathFormat"));
                var mediaType = response.Content.Headers.ContentType.MediaType;
                var stream    = await response.Content.ReadAsStreamAsync();

                var savePath = Path.Combine(AppContext.BaseDirectory + "wwwroot", ServerUrl);
                if (string.IsNullOrEmpty(Path.GetExtension(savePath)))
                {
                    savePath  = savePath + MimeMapper.ExtTypes[mediaType];
                    ServerUrl = ServerUrl + MimeMapper.ExtTypes[mediaType];
                }

                var(url, success) = await Startup.ServiceProvider.GetRequiredService <ImagebedClient>().UploadImage(stream, savePath);

                if (success)
                {
                    ServerUrl = url;
                }
                else
                {
                    if (!Directory.Exists(Path.GetDirectoryName(savePath)))
                    {
                        Directory.CreateDirectory(Path.GetDirectoryName(savePath));
                    }

                    var ms = new MemoryStream();
                    await stream.CopyToAsync(ms);

                    await File.WriteAllBytesAsync(savePath, ms.GetBuffer());
                }
                stream.Close();
                await stream.DisposeAsync();

                State = "SUCCESS";
            }
            catch (Exception e)
            {
                State = "抓取错误:" + e.Message;
                LogManager.Error(e);
            }

            return(this);
        }
Пример #5
0
        public async Task <Crawler> Fetch()
        {
            if (!SourceUrl.IsExternalAddress())
            {
                State = "INVALID_URL";
                return(this);
            }
            try
            {
                using var response = await _httpClient.GetAsync(SourceUrl);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    State = "Url returns " + response.StatusCode;
                    return(this);
                }

                ServerUrl = PathFormatter.Format(Path.GetFileNameWithoutExtension(SourceUrl), CommonHelper.SystemSettings.GetOrAdd("UploadPath", "upload").Trim('/', '\\') + UeditorConfig.GetString("catcherPathFormat")) + MimeMapper.ExtTypes[response.Content.Headers.ContentType?.MediaType ?? "image/jpeg"];
                await using var stream = await response.Content.ReadAsStreamAsync();

                var savePath = Path.Combine(AppContext.BaseDirectory + "wwwroot", ServerUrl);
                var(url, success) = await Startup.ServiceProvider.GetRequiredService <ImagebedClient>().UploadImage(stream, savePath, default);

                if (success)
                {
                    ServerUrl = url;
                }
                else
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(savePath));
                    await File.WriteAllBytesAsync(savePath, await stream.ToArrayAsync());
                }
                State = "SUCCESS";
            }
            catch (Exception e)
            {
                State = "抓取错误:" + e.Message;
                LogManager.Error(e);
            }

            return(this);
        }