예제 #1
0
        public override Task <string> Process()
        {
            try
            {
                _start = string.IsNullOrEmpty(Request.Query["start"]) ? 0 : Convert.ToInt32(Request.Query["start"]);
                _size  = string.IsNullOrEmpty(Request.Query["size"]) ? UeditorConfig.GetInt("imageManagerListSize") : Convert.ToInt32(Request.Query["size"]);
            }
            catch (FormatException)
            {
                _state = ResultState.InvalidParam;
                return(Task.FromResult(WriteResult()));
            }
            var buildingList = new List <string>();

            try
            {
                var localPath = AppContext.BaseDirectory + "wwwroot" + _pathToList;
                buildingList.AddRange(Directory.GetFiles(localPath, "*", SearchOption.AllDirectories).Where(x => _searchExtensions.Contains(Path.GetExtension(x).ToLower())).Select(x => _pathToList + x.Substring(localPath.Length).Replace("\\", "/")));
                _total    = buildingList.Count;
                _fileList = buildingList.OrderBy(x => x).Skip(_start).Take(_size).ToArray();
            }
            catch (UnauthorizedAccessException)
            {
                _state = ResultState.AuthorizError;
            }
            catch (DirectoryNotFoundException)
            {
                _state = ResultState.PathNotFound;
            }
            catch (IOException)
            {
                _state = ResultState.IOError;
            }
            return(Task.FromResult(WriteResult()));
        }
예제 #2
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);
            }
        }
예제 #3
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);
            }
        }
예제 #4
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);
        }
예제 #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.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);
        }
예제 #6
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);
        }