Exemplo n.º 1
0
        public Crawler Fetch()
        {
            if (!(SourceUrl.IsExternalAddress()))
            {
                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"));
                var savePath = Server.MapPath(ServerUrl);
                if (!Directory.Exists(Path.GetDirectoryName(savePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(savePath));
                }
                try
                {
                    using (var stream = response.GetResponseStream())
                    {
                        using (var ms = new MemoryStream())
                        {
                            stream.CopyTo(ms);
                            File.WriteAllBytes(savePath, ms.GetBuffer());
                        }
                        //var (url, success) = CommonHelper.UploadImage(savePath);
                        //if (success)
                        //{
                        //    ServerUrl = url;
                        //    BackgroundJob.Enqueue(() => File.Delete(savePath));
                        //}
                    }
                    State = "SUCCESS";
                }
                catch (Exception e)
                {
                    State = "抓取错误:" + e.Message;
                }
                return(this);
            }
        }
Exemplo n.º 2
0
        public override void Process()
        {
            try
            {
                _start = string.IsNullOrEmpty(Request["start"]) ? 0 : Convert.ToInt32(Request["start"]);
                _size  = string.IsNullOrEmpty(Request["size"]) ? UeditorConfig.GetInt("imageManagerListSize") : Convert.ToInt32(Request["size"]);
            }
            catch (FormatException)
            {
                _state = ResultState.InvalidParam;
                WriteResult();
                return;
            }
            var buildingList = new List <string>();

            try
            {
                var localPath = Server.MapPath(_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;
            }
            finally
            {
                WriteResult();
            }
        }