예제 #1
0
        public async Task UpdateNews(int newsid, int column, int language, string title, string subTitle, string content, bool picnews)
        {
            var news = await _dbContext.News.AsNoTracking().FirstOrDefaultAsync(n => n.NewsID == newsid);

            if (news != null)
            {
                var fileName = string.Empty;
                var file     = _accessor.HttpContext.Request.Form.Files["file"];
                if (file != null)
                {
                    var fileExtension = Path.GetExtension(file.FileName);
                    fileName = $"{DateTime.Now.ToString("yyMMddHHmmssfff")}{fileExtension}";
                    string webRootPath  = _hostingEnvironment.WebRootPath;//获取物理地址
                    string path         = $"upload/image/{fileName}";
                    var    memoryStream = new MemoryStream();
                    await file.CopyToAsync(memoryStream);

                    if (picnews)
                    {
                        _pictureHelper.ProcessByStream(memoryStream, new PictureSize
                        {
                            Width  = 170,
                            Height = 130,
                            Mode   = "Cut"
                        });
                        memoryStream.Dispose();
                        memoryStream = _pictureHelper.Ms;
                    }
                    byte[] bytes = new byte[memoryStream.Length];
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    memoryStream.Read(bytes, 0, bytes.Length);
                    memoryStream.Dispose();
                    await File.WriteAllBytesAsync(Path.Combine(webRootPath, path), bytes);

                    await _uploadService.SaveToRemotePath(bytes, path);//保存到远程路径

                    await _uploadService.addFile(fileName, fileExtension, "News");

                    news.NewsImageName = fileName;
                }
                news.ColumnID   = column;
                news.Language   = language;
                news.Title      = title;
                news.SubTitle   = subTitle;
                news.NewsDetail = content;
                _dbContext.Update(news);
                await _dbContext.SaveChangesAsync();
            }
        }
예제 #2
0
        public async Task <Crawler> Fetch(string sourceUrl)
        {
            this.sourceUrl = sourceUrl;
            if (!IsExternalIPAddress(this.sourceUrl))
            {
                state = "INVALID_URL";
                return(this);
            }
            var request = WebRequest.Create(this.sourceUrl) as WebRequest;

            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 = Server.MapPath(ServerUrl);
                var savePath = Path.Combine(_hostingEnvironment.WebRootPath, serverUrl);

                string extension = Path.GetExtension(serverUrl);
                string filename  = Path.GetFileName(serverUrl);

                if (!Directory.Exists(Path.GetDirectoryName(savePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(savePath));
                }
                try
                {
                    byte[]    uploadFileBytes = null;
                    WebClient webClient       = new WebClient();
                    webClient.Credentials = CredentialCache.DefaultCredentials;
                    //var memoryStream = await webClient.OpenWriteTaskAsync(this.sourceUrl);
                    byte[] bytes = await webClient.DownloadDataTaskAsync(this.sourceUrl);

                    var memoryStream = new MemoryStream(bytes);
                    webClient.Dispose();

                    _pictureHelper.ProcessByStream(memoryStream, new PictureSize
                    {
                        Width  = 700,
                        Height = 700,
                        Mode   = "Auto"
                    });
                    memoryStream.Dispose();
                    memoryStream    = _pictureHelper.Ms;
                    uploadFileBytes = new byte[memoryStream.Length];
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    memoryStream.Read(uploadFileBytes, 0, uploadFileBytes.Length);
                    memoryStream.Dispose();

                    await File.WriteAllBytesAsync(savePath, uploadFileBytes);

                    await _uploadService.SaveToRemotePath(uploadFileBytes, serverUrl);//保存到远程路径

                    await _uploadService.addFile(filename, extension, "Ueditor");

                    state = "SUCCESS";
                }
                catch (Exception e)
                {
                    state = "抓取错误:" + e.Message;
                }
                return(this);
            }
        }
예제 #3
0
        public async Task Process(UploadConfig uploadConfig)
        {
            this._uploadConfig = uploadConfig;
            byte[] uploadFileBytes = null;
            string uploadFileName  = null;

            if (_uploadConfig.Base64)
            {
                uploadFileName  = _uploadConfig.Base64Filename;
                uploadFileBytes = Convert.FromBase64String(_accessor.HttpContext.Request.Query[_uploadConfig.UploadFieldName]);//注意可能会错
            }
            else
            {
                var file = _accessor.HttpContext.Request.Form.Files[_uploadConfig.UploadFieldName];
                uploadFileName = file.FileName;

                if (!CheckFileType(uploadFileName))
                {
                    this._result.State = UploadState.TypeNotAllow;
                    await WriteResult();

                    return;
                }
                if (!CheckFileSize(file.Length))
                {
                    this._result.State = UploadState.SizeLimitExceed;
                    await WriteResult();

                    return;
                }
                //uploadFileBytes = new byte[file.Length];
                try
                {
                    var memoryStream = new MemoryStream();
                    await file.CopyToAsync(memoryStream);

                    if (_uploadConfig.ActionName == "uploadimage")
                    {
                        _pictureHelper.ProcessByStream(memoryStream, new PictureSize
                        {
                            Width  = 700,
                            Height = 700,
                            Mode   = "Auto"
                        });
                        memoryStream.Dispose();
                        memoryStream = _pictureHelper.Ms;
                    }
                    uploadFileBytes = new byte[memoryStream.Length];
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    memoryStream.Read(uploadFileBytes, 0, uploadFileBytes.Length);
                    memoryStream.Dispose();
                    //file.InputStream.Read(uploadFileBytes, 0, file.Length);
                    //file.OpenReadStream().Read(uploadFileBytes, 0, uploadFileBytes.Length);
                }
                catch (Exception)
                {
                    _result.State = UploadState.NetworkError;
                    await WriteResult();
                }
            }
            _result.OriginFileName = uploadFileName;

            var savePath = PathFormatter.Format(uploadFileName, _uploadConfig.PathFormat);

            string extension = Path.GetExtension(uploadFileName);
            string filename  = Path.GetFileName(savePath);

            var localPath = Path.Combine(_hostingEnvironment.WebRootPath, savePath);

            try
            {
                if (!Directory.Exists(Path.GetDirectoryName(localPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(localPath));
                }

                //File.WriteAllBytes(localPath, uploadFileBytes);
                await File.WriteAllBytesAsync(localPath, uploadFileBytes);

                await _uploadService.SaveToRemotePath(uploadFileBytes, savePath);//保存到远程路径

                await _uploadService.addFile(filename, extension, "Ueditor");

                _result.Url   = savePath;
                _result.State = UploadState.Success;
            }
            catch (Exception e)
            {
                _result.State        = UploadState.FileAccessError;
                _result.ErrorMessage = e.Message;
            }
            finally
            {
                await WriteResult();
            }
        }
예제 #4
0
        public async Task AddPages(PageView pv)
        {
            if (pv.PageImg != null)
            {
                var    fileExtension = Path.GetExtension(pv.PageImg.FileName);
                var    fileName      = $"{DateTime.Now.ToString("yyMMddHHmmssfff")}{fileExtension}";
                string webRootPath   = _hostingEnvironment.WebRootPath;//获取物理地址
                string path          = $"upload/image/{fileName}";
                //string contentRootPath = _hostingEnvironment.ContentRootPath;

                var memoryStream = new MemoryStream();
                await pv.PageImg.CopyToAsync(memoryStream);

                if (pv.IsPicturePage)
                {
                    _pictureHelper.ProcessByStream(memoryStream, new PictureSize
                    {
                        Width  = 200,
                        Height = 255,
                        Mode   = "Cut"
                    });
                    memoryStream.Dispose();
                    memoryStream = _pictureHelper.Ms;
                }
                byte[] bytes = new byte[memoryStream.Length];
                // 设置当前流的位置为流的开始
                memoryStream.Seek(0, SeekOrigin.Begin);
                memoryStream.Read(bytes, 0, bytes.Length);
                memoryStream.Dispose();
                //用下面的就不需要先seek
                //nv.NewsImg.OpenReadStream().Read(bytes, 0, bytes.Length);
                await File.WriteAllBytesAsync(Path.Combine(webRootPath, path), bytes);

                await _uploadService.SaveToRemotePath(bytes, path);//保存到远程路径

                //memoryStream.Dispose();



                _dbContext.Pages.Add(new Pages
                {
                    ColumnID      = int.Parse(pv.Column),
                    Language      = (int)pv.Language,
                    Title         = pv.Title,
                    PContent      = pv.PContent,
                    PageImageName = fileName,
                });
                await _dbContext.SaveChangesAsync();

                await _uploadService.addFile(fileName, fileExtension, "Page");
            }
            else
            {
                _dbContext.Pages.Add(new Pages
                {
                    ColumnID = int.Parse(pv.Column),
                    Language = (int)pv.Language,
                    Title    = pv.Title,
                    PContent = pv.PContent,
                });
                await _dbContext.SaveChangesAsync();
            }
        }