public async Task <IActionResult> UploadImage(string callback)
        {
            var result = new UploadResult();

            var files = Request.Form.Files;

            if (files.Count == 0 || files[0].Length == 0)
            {
                result.State = GetStateMessage(UploadState.Unknown);
                return(WriteResult(callback, result));
            }

            var file = files[0];

            result.OriginFileName = Path.GetFileName(file.FileName);
            var fileExt = Path.GetExtension(result.OriginFileName);

            if (!CheckFileType(fileExt, _config.imageAllowFiles))
            {
                result.State = GetStateMessage(UploadState.TypeNotAllow);
                return(WriteResult(callback, result));
            }

            if (!CheckFileSize(file.Length, _config.imageMaxSize))
            {
                result.State = GetStateMessage(UploadState.SizeLimitExceed);
                return(WriteResult(callback, result));
            }

            var newFileName = Guid.NewGuid() + fileExt;
            var path        = "/" + DateTime.Today.ToString("yyMM") + "/" + newFileName;

            using (var stream = file.OpenReadStream())
            {
                var bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);

                var success = await _upYun.WriteFileAsync(path, bytes, true);

                if (success)
                {
                    result.State = GetStateMessage(UploadState.Success);
                    result.Url   = _upYun.Domain + path;
                    return(WriteResult(callback, result));
                }
                else
                {
                    result.State = GetStateMessage(UploadState.Unknown);
                    result.Url   = "";
                    return(WriteResult(callback, result));
                }
            }
        }
Exemplo n.º 2
0
        public Crawler Fetch()
        {
            if (!IsExternalIpAddress(SourceUrl))
            {
                State = "INVALID_URL";
                return(this);
            }
            var request = WebRequest.Create(SourceUrl) as HttpWebRequest;

            // ReSharper disable once PossibleNullReferenceException
            using (var response = request.GetResponse() as HttpWebResponse)
            {
                // ReSharper disable once PossibleNullReferenceException
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    State = "Url returns " + response.StatusCode + ", " + response.StatusDescription;
                    return(this);
                }
                if (response.ContentType.IndexOf("image", StringComparison.Ordinal) == -1)
                {
                    State = "Url is not an image";
                    return(this);
                }

                try
                {
                    var    stream = response.GetResponseStream();
                    var    reader = new BinaryReader(stream);
                    byte[] bytes;
                    using (var ms = new MemoryStream())
                    {
                        var buffer = new byte[4096];
                        int count;
                        while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            ms.Write(buffer, 0, count);
                        }
                        bytes = ms.ToArray();
                    }

                    ServerUrl = PathFormatter.Format(Path.GetFileName(SourceUrl), _config.catcherPathFormat);
                    if (_client == null)
                    {
                        var savePath = Path.Combine(_webRootPath, ServerUrl);
                        if (!Directory.Exists(Path.GetDirectoryName(savePath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(savePath));
                        }

                        File.WriteAllBytes(savePath, bytes);
                        State = "SUCCESS";
                    }
                    else
                    {
                        var result = _client.WriteFileAsync("/" + ServerUrl, bytes, true).Result;
                        if (result)
                        {
                            State = "SUCCESS";
                        }
                        else
                        {
                            State = "上传失败";
                        }
                    }
                }
                catch (Exception e)
                {
                    State = "抓取错误:" + e.Message;
                }
                return(this);
            }
        }