Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        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 = AppContext.BaseDirectory + "wwwroot" + 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
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public override 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(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;
            }
            var result = WriteResult();

            return(result);
        }