예제 #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), EditorConfig.GetString("catcherPathFormat"));
                var savePath = Path.Combine(Directory.GetCurrentDirectory(), 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);
            }
        }
예제 #2
0
        public override void Process()
        {
            try
            {
                Start = String.IsNullOrEmpty(Request.GetValue("start")) ? 0 : Convert.ToInt32(Request.GetValue("start"));
                Size  = String.IsNullOrEmpty(Request.GetValue("size")) ? EditorConfig.GetInt("imageManagerListSize") : Convert.ToInt32(Request.GetValue("size"));
            }
            catch (FormatException)
            {
                State = ResultState.InvalidParam;
                WriteResult();
                return;
            }
            var buildingList = new List <String>();

            try
            {
                var localPath = Path.Combine(Directory.GetCurrentDirectory(), PathToList);
                //buildingList.AddRange(Directory.GetFiles(localPath, "*", SearchOption.AllDirectories)
                //    .Where(x => SearchExtensions.Contains(Path.GetExtension(x).ToLower()))
                //    .Select(x => PathToList + x.Substring(localPath.Length).Replace("\\", "/")));
                string imgListUrl = "";//System.Configuration.ConfigurationManager.AppSettings["ImageListUrl"];
                Dictionary <string, string> parameters = new Dictionary <string, string>()
                {
                    { "path", @"Editor\upload\image" },
                    { "suffixFilter", ".png,.jpg,.jpeg,.gif,.bmp" }
                };
                string   imgPathString = string.Empty; //WebRequestHelper.GetValue(imgListUrl, parameters);
                string[] pathArray     = imgPathString.LSplit(",");
                buildingList.AddRange(pathArray);
                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();
            }
        }