示例#1
0
    private void DoDownloadAndConvert()
    {
        DownloadParams param = new DownloadParams();

        param.BaseURL  = edtDownloadLink.Text;
        param.FileName = "stockQuotes_%mm%dd%yyyy";
        param.FromDate = Convert.ToDateTime(edtFrom.Text);
        param.ToDate   = Convert.ToDateTime(edtTo.Text);
        string           savePath   = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/Reports";
        ReportDownloader downloader = new ReportDownloader(param, savePath, ProcessDownloadedFile);

        downloader.OnReportDownloadCompletedEvent += (s, o) =>
        {
        };
        downloader.OnDownloadAllCompletedEvent += (s, e) =>
        {
        };
        downloader.OnDownloadAllCompletedEvent += (s, e) => {
            //MonoPSEGetDataService dataService = new MonoPSEGetDataService();
            //dataService.
        };

        downloader.OnStartDownloadProcess += (s, o) => {
            labelStatus.Text = "Trying...";
        };
        downloader.OnDownloadProgressEvent += (s, o) => {
        };
        downloader.Download();
    }
示例#2
0
文件: Program.cs 项目: yvdjee/PSEGet
        static void WorkIt()
        {
            if (_targetPath == null)
            {
                throw new Exception("Error: Unspecified PSE report file path.");
            }

            if (_targetPath.Contains("http:"))
            {
                var downloadParams = new DownloadParams();
                downloadParams.BaseURL  = _targetPath;
                downloadParams.FileName = "stockQuotes_%mm%dd%yyyy";
                downloadParams.FromDate = Convert.ToDateTime(_dateFrom);
                downloadParams.ToDate   = Convert.ToDateTime(_dateTo);

                var downloader = new ReportDownloader(downloadParams, _reportsDir, null);
                downloader.AsyncMode = false;
                downloader.Download();

                foreach (DownloadFileInfo reportFile in downloader.DownloadedFiles)
                {
                    if (reportFile.Success)
                    {
                        ConvertIt(reportFile.Filename);
                    }
                }
            }
            else
            {
                ConvertIt(_targetPath);
            }
        }
示例#3
0
        private void AsyncCallBack(IAsyncResult ar)
        {
            try
            {
                if (ar.IsCompleted)
                {
                    DownloadParams  dp       = ar.AsyncState as DownloadParams;
                    HttpWebResponse response = dp.Request.EndGetResponse(ar) as HttpWebResponse;
                    Stream          stream   = response.GetResponseStream();

                    int          length = (int)response.ContentLength;
                    BinaryReader br     = new BinaryReader(stream);
                    FileStream   fs     = File.Create(dp.DestFilePath ?? "temp.zip");
                    fs.Write(br.ReadBytes(length), 0, length);
                    br.Close();
                    fs.Close();
                    response.Close();

                    if (_OnDownloadFinished != null)
                    {
                        _OnDownloadFinished(this, null);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
示例#4
0
        static void WorkIt()
        {
            if (_targetPath == null)
            {
                throw new Exception("Error: Unspecified PSE report file path.");
            }

            if (_targetPath.Contains("https:"))
            {
                if (!_useLocal)
                {
                    var downloadParams = new DownloadParams();
                    downloadParams.BaseURL  = _targetPath;
                    downloadParams.FileName = "stockQuotes_%mm%dd%yyyy";
                    downloadParams.FromDate = Convert.ToDateTime(_dateFrom);
                    downloadParams.ToDate   = Convert.ToDateTime(_dateTo);

                    var downloader = new ReportDownloader(downloadParams, _reportsDir, null);
                    downloader.AsyncMode = false;
                    downloader.Download();

                    Console.WriteLine("---");

                    int count = 0;
                    int len   = downloader.DownloadedFiles.Count;
                    foreach (DownloadFileInfo reportFile in downloader.DownloadedFiles)
                    {
                        count++;
                        float progress = count > 0 ? ((float)count / (float)len) * 100f : 0f;
                        Console.Write($"Converting {Path.GetFileNameWithoutExtension(reportFile.Filename)} [{count}/{len}] ({progress.ToString("0")}%) ... ");
                        if (reportFile.Success)
                        {
                            ConvertIt(reportFile.Filename);
                            Console.WriteLine("SUCCESS!");
                        }
                        else
                        {
                            Console.WriteLine("FAILED!");
                        }
                    }
                }
                else
                {
                    int count = 0;
                    int len   = Directory.GetFiles(_reportsDir).Length;
                    foreach (string file in Directory.GetFiles(_reportsDir))
                    {
                        count++;
                        float progress = count > 0 ? ((float)count / (float)len) * 100f : 0f;
                        Console.Write($"Converting {Path.GetFileNameWithoutExtension(file)} [{count}/{len}] ({progress.ToString("0")}%) ... ");
                        ConvertIt(file);
                        Console.WriteLine("SUCCESS!");
                    }
                }
            }
            else
            {
                ConvertIt(_targetPath);
            }
        }
示例#5
0
        private void AsyncCallBack(IAsyncResult ar)
        {
            try
            {
                if (ar.IsCompleted)
                {
                    DownloadParams  dp       = ar.AsyncState as DownloadParams;
                    HttpWebResponse response = dp.Request.EndGetResponse(ar) as HttpWebResponse;
                    Stream          stream   = response.GetResponseStream();

                    int length = (int)response.ContentLength;

                    byte[] buffer = new byte[BufferSize];
                    int    count  = 0;
                    using (FileStream filestream = new FileStream(dp.DestFilePath ?? "temp.zip", FileMode.Create))
                    {
                        int downloaded = 0;
                        while (true)
                        {
                            count       = stream.Read(buffer, 0, BufferSize);
                            downloaded += count;

                            if (count > 0)
                            {
                                //notice
                                filestream.Write(buffer, 0, count);
                                _context.Post(d => { this.lb_progress.Text = d + "%"; }, downloaded * 100 / length);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    response.Close();

                    //unzip
                    ZipHelper.UnZip(dp.DestFilePath, Application.StartupPath);
                    File.Delete(dp.DestFilePath);

                    if (!string.IsNullOrEmpty(ProcessName))
                    {
                        //run controlchecker
                        Process process = new Process();
                        process.StartInfo.FileName = ProcessName;
                        process.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Url);
            }
            finally
            {
                Application.Exit();
                Environment.Exit(0);
            }
        }
示例#6
0
        public override void Validate(string propertyName = null)
        {
            base.Validate(propertyName);

            string currentProperty = nameof(DownloadParams);

            if (string.IsNullOrWhiteSpace(propertyName) || propertyName == currentProperty)
            {
                DownloadParams?.Validate();

                if (_twitchService.IsFileNameUsed(_downloadParams.FullPath))
                {
                    DownloadParams.AddError(nameof(DownloadParams.Filename), "Another video is already being downloaded to this file!");
                }

                if (DownloadParams.HasErrors)
                {
                    AddError(currentProperty, "Invalid Download Parameters!");

                    if (DownloadParams.GetErrors(nameof(DownloadParameters.CropStartTime)) is List <string> cropStartErrors && cropStartErrors.Count > 0)
                    {
                        string firstError = cropStartErrors.First();
                        AddError(nameof(CropStartHours), firstError);
                        AddError(nameof(CropStartMinutes), firstError);
                        AddError(nameof(CropStartSeconds), firstError);
                    }

                    if (DownloadParams.GetErrors(nameof(DownloadParameters.CropEndTime)) is List <string> cropEndErrors && cropEndErrors.Count > 0)
                    {
                        string firstError = cropEndErrors.First();
                        AddError(nameof(CropEndHours), firstError);
                        AddError(nameof(CropEndMinutes), firstError);
                        AddError(nameof(CropEndSeconds), firstError);
                    }

                    if (DownloadParams.GetErrors(nameof(DownloadParameters.AutoSplitTime)) is List <string> autoSplitTimeErrors && autoSplitTimeErrors.Count > 0)
                    {
                        string firstError = autoSplitTimeErrors.First();
                        AddError(nameof(AutoSplitTimeHours), firstError);
                        AddError(nameof(AutoSplitTimeMinutes), firstError);
                        AddError(nameof(AutoSplitTimeSeconds), firstError);
                        AddError(nameof(AutoSplitUseExtended), firstError);
                    }

                    if (DownloadParams.GetErrors(nameof(DownloadParameters.AutoSplitOverlap)) is List <string> overlapErrors && overlapErrors.Count > 0)
                    {
                        string firstError = overlapErrors.First();
                        AddError(nameof(AutoSplitUseExtended), firstError);
                    }
                }
            }
        }
示例#7
0
        async Task <Stream> IDownloader.Download(DownloadParams downloadParams)
        {
            if (downloadParams.CacheMode == CacheMode.AllowCacheReading || downloadParams.CacheMode == CacheMode.DownloadFromCacheOnly)
            {
                var cachedValue = cache.GetValue(downloadParams.Location);
                if (cachedValue != null)
                {
                    tracer.Info("found in cache content for location='{0}'", downloadParams.Location);
                    return(cachedValue);
                }
                if (downloadParams.CacheMode == CacheMode.DownloadFromCacheOnly)
                {
                    return(null);
                }
            }
            var task = new PendingTask()
            {
                location         = downloadParams.Location,
                expectedMimeType = downloadParams.ExpectedMimeType,
                cancellation     = downloadParams.Cancellation,
                progress         = downloadParams.Progress,
                isLoginUrl       = downloadParams.IsLoginUrl,
                stream           = new MemoryStream(),
                promise          = new TaskCompletionSource <Stream>(),
            };

            lock (syncRoot)
            {
                tracer.Info("new task {0} added for location='{1}'", task, task.location);
                tasks.Enqueue(task);
            }
            TryTakeNewTask();
            var stream = await task.promise.Task;

            if (stream != null)
            {
                bool setCache = true;
                if (downloadParams.AllowCacheWriting != null)
                {
                    stream.Position = 0;
                    setCache        = downloadParams.AllowCacheWriting(stream);
                }
                if (setCache)
                {
                    stream.Position = 0;
                    await cache.SetValue(downloadParams.Location, stream);
                }
                stream.Position = 0;
            }
            return(stream);
        }
示例#8
0
        public override void Validate(string propertyName = null)
        {
            base.Validate(propertyName);

            string currentProperty = nameof(DownloadParams);

            if (string.IsNullOrWhiteSpace(propertyName) || propertyName == currentProperty)
            {
                DownloadParams?.Validate();

                if (_twitchService.IsFileNameUsed(_downloadParams.FullPath))
                {
                    DownloadParams.AddError(nameof(DownloadParams.Filename), "Another video is already being downloaded to this file!");
                }

                if (DownloadParams.HasErrors)
                {
                    AddError(currentProperty, "Invalid Download Parameters!");
                }
            }

            currentProperty = nameof(CropStartTime);

            if (string.IsNullOrWhiteSpace(propertyName) || propertyName == currentProperty)
            {
                DownloadParams?.Validate(currentProperty);

                if (DownloadParams.HasErrors)
                {
                    if (DownloadParams.GetErrors(currentProperty) is List <string> errors && errors.Count > 0)
                    {
                        AddError(currentProperty, errors.First());
                    }
                }
            }

            currentProperty = nameof(CropEndTime);

            if (string.IsNullOrWhiteSpace(propertyName) || propertyName == currentProperty)
            {
                DownloadParams?.Validate(currentProperty);

                if (DownloadParams.HasErrors)
                {
                    if (DownloadParams.GetErrors(currentProperty) is List <string> errors && errors.Count > 0)
                    {
                        AddError(currentProperty, errors.First());
                    }
                }
            }
        }
示例#9
0
        public void Download(string destPath)
        {
            HttpWebRequest request = WebRequest.Create(updateEntity.packagepath) as HttpWebRequest;

            if (request == null)
            {
                return;
            }

            DownloadParams dp = new DownloadParams();

            dp.Request      = request;
            dp.DestFilePath = destPath;

            request.BeginGetResponse(AsyncCallBack, dp);
        }
示例#10
0
        private void Download(string url)
        {
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            request.Timeout = 10000;    //10s超时
            if (request == null)
            {
                return;
            }

            DownloadParams dp = new DownloadParams();

            dp.Request      = request;
            dp.DestFilePath = Path.Combine(Application.StartupPath, "temp.zip");

            request.BeginGetResponse(AsyncCallBack, dp);
        }
示例#11
0
        public override void Validate(string propertyName = null)
        {
            base.Validate(propertyName);

            string currentProperty = nameof(DownloadParams);

            if (string.IsNullOrWhiteSpace(propertyName) || propertyName == currentProperty)
            {
                DownloadParams?.Validate();

                if (_downloadParams.FullPath.Length > 250)
                {
                    DownloadParams.AddError(nameof(DownloadParams.Filename), "The full filename (directory + filename) must be shorter than 250 characters!");
                }

                if (_downloadService.IsFileNameUsed(_downloadParams.FullPath))
                {
                    DownloadParams.AddError(nameof(DownloadParams.Filename), "Another video is already being downloaded to this file!");
                }

                if (DownloadParams.HasErrors)
                {
                    AddError(currentProperty, "Invalid Download Parameters!");

                    if (DownloadParams.GetErrors(nameof(DownloadParameters.CropStartTime)) is List <string> cropStartErrors && cropStartErrors.Count > 0)
                    {
                        string firstError = cropStartErrors.First();
                        AddError(nameof(CropStartHours), firstError);
                        AddError(nameof(CropStartMinutes), firstError);
                        AddError(nameof(CropStartSeconds), firstError);
                    }

                    if (DownloadParams.GetErrors(nameof(DownloadParameters.CropEndTime)) is List <string> cropEndErrors && cropEndErrors.Count > 0)
                    {
                        string firstError = cropEndErrors.First();
                        AddError(nameof(CropEndHours), firstError);
                        AddError(nameof(CropEndMinutes), firstError);
                        AddError(nameof(CropEndSeconds), firstError);
                    }
                }
            }
        }
示例#12
0
        string DownloadFiles(CallType callType, string fileName = "file", int count = 1, int threads = 1)
        {
            InitConsole();

            string name;
            string ext;

            if (callType == CallType.Console)
            {
                name = ConfigurationManager.AppSettings["SourcePath"] + Path.GetFileNameWithoutExtension(fileName);;
                ext  = !string.IsNullOrEmpty(Path.GetExtension(fileName)) ? Path.GetExtension(fileName) : "docx";
            }
            else
            {
                name = fileName;
                ext  = "";
            }

            var total      = count;
            var offset     = total / threads;
            var parameters = new DownloadParams
            {
                Offset   = 0,
                Count    = offset,
                Config   = _config,
                FileName = name,
                FileType = ext
            };

            //var download = new DownloadThread("THREAD " + 0 + ", View PDF of " + offset + " DOCX", parameters);

            for (int i = 0; i < threads; i++)
            {
                parameters.Offset = i * offset;
                var download = new DownloadThread("THREAD " + i + ", Download of " + offset + " DOCX", parameters);
            }
            return("Download success!");
        }
        public override void Validate(string propertyName = null)
        {
            base.Validate(propertyName);

            string currentProperty = nameof(DownloadParams);

            if (string.IsNullOrWhiteSpace(propertyName) || propertyName == currentProperty)
            {
                DownloadParams?.Validate();

                if (_twitchService.IsFileNameUsed(_downloadParams.FullPath))
                {
                    DownloadParams.AddError(nameof(DownloadParams.Filename), "파일을 사용중입니다!");
                }

                if (DownloadParams.HasErrors)
                {
                    AddError(currentProperty, "잘못된 다운로드 매개 변수입니다!");

                    if (DownloadParams.GetErrors(nameof(DownloadParameters.CropStartTime)) is List <string> cropStartErrors && cropStartErrors.Count > 0)
                    {
                        string firstError = cropStartErrors.First();
                        AddError(nameof(CropStartHours), firstError);
                        AddError(nameof(CropStartMinutes), firstError);
                        AddError(nameof(CropStartSeconds), firstError);
                    }

                    if (DownloadParams.GetErrors(nameof(DownloadParameters.CropEndTime)) is List <string> cropEndErrors && cropEndErrors.Count > 0)
                    {
                        string firstError = cropEndErrors.First();
                        AddError(nameof(CropEndHours), firstError);
                        AddError(nameof(CropEndMinutes), firstError);
                        AddError(nameof(CropEndSeconds), firstError);
                    }
                }
            }
        }
示例#14
0
        public string ViewPDF(CallType callType, UploadParams parameters)
        {
            InitConsole();

            string name;
            string ext;

            if (callType == CallType.Console)
            {
                name = ConfigurationManager.AppSettings["SourcePath"] + Path.GetFileNameWithoutExtension(parameters.FileName);;
                ext  = !string.IsNullOrEmpty(Path.GetExtension(parameters.FileName)) ? Path.GetExtension(parameters.FileName) : "docx";
            }
            else
            {
                name = parameters.FileName;
                ext  = "";
            }

            var total     = parameters.Count;
            var offset    = total / parameters.Threads;
            var parameter = new DownloadParams
            {
                Offset   = 0,
                Count    = offset,
                Config   = _config,
                FileName = name,
                FileType = ext
            };

            parameters.Config = _config;

            switch (ext == "" ? Path.GetExtension(parameters.FileName).Remove(0, 1) : ext)
            {
            case "docx":
            case "doc":
            case "docm":
            case "dot":
            case "dotm":
            case "dotx":
            case "odt":
            case "rtf":
                ConvertWordToPdf(name, ext, parameters.Count, parameters.Threads);
                break;

            case "xls":
            case "xlsx":
            case "xlsm":
            case "xlsb":
            case "xml":
            case "csv":
            case "mht":
            case "ods":
            case "prn":
            case "slk":
                ConvertExcelToPdf(name, ext, parameters.Count, parameters.Threads);
                break;

            case "ppt":
            case "pptx":
            case "pptm":
            case "pps":
            case "ppsm":
            case "ppsx":
            case "odp":
                ConvertPowerPointToPdf(name, ext, parameters.Count, parameters.Threads);
                break;
            }
            return("Convert Success!");
        }
示例#15
0
        string ConvertToPDF(CallType callType, string fileName = "file", int count = 1, int threads = 1)
        {
            InitConsole();

            string name;
            string ext;

            if (callType == CallType.Console)
            {
                name = ConfigurationManager.AppSettings["SourcePath"] + Path.GetFileNameWithoutExtension(fileName);;
                ext  = !string.IsNullOrEmpty(Path.GetExtension(fileName)) ? Path.GetExtension(fileName) : "docx";
            }
            else
            {
                name = fileName;
                ext  = "";
            }

            var parametersDocx = new ConvertParams
            {
                Count          = count,
                Config         = _config,
                ResultFileName = name,
                FileType       = ext
            };

            var total      = count;
            var offset     = total / threads;
            var parameters = new DownloadParams
            {
                Offset   = 0,
                Count    = offset,
                Config   = _config,
                FileName = name,
                FileType = ext
            };

            for (int i = 0; i < threads; i++)
            {
                parameters.Offset = i * offset;
                var uploadDocx1 = new WordConvertThread("Convert DOCX", parametersDocx);
            }
            return("Convert success!");

            //var parametersXlsx = new ConvertParams
            //{
            //    Count = 500,
            //    Config = _config,
            //    ResultFileName = "New",
            //    FileType = "xlsx"
            //};
            //var parametersPptx = new ConvertParams
            //{
            //    Count = 500,
            //    Config = _config,
            //    ResultFileName = "New",
            //    FileType = "pptx"
            //};

            //var uploadDocx1 = new WordConvertThread("Convert first 500 DOCX", parametersDocx);
            //parametersDocx.ResultFileName = "New1";
            //var uploadDocx2 = new WordConvertThread("Convert second 500 DOCX", parametersDocx);
            //parametersDocx.ResultFileName = "New2";
            //var uploadDocx3 = new WordConvertThread("Convert third 500 DOCX", parametersDocx);

            //var uploadXlsx1 = new WordConvertThread("Convert first 500 XLSX", parametersXlsx);
            //parametersXlsx.ResultFileName = "New1";
            //var uploadXlsx2 = new WordConvertThread("Convert second 500 XLSX", parametersXlsx);
            //parametersXlsx.ResultFileName = "New2";
            //var uploadXlsx3 = new WordConvertThread("Convert third 500 XLSX", parametersXlsx);

            //var uploadPptx1 = new WordConvertThread("Convert first 500 PPTX", parametersPptx);
            //parametersPptx.ResultFileName = "New1";
            //var uploadPptx2 = new WordConvertThread("Convert second 500 PPTX", parametersPptx);
            //parametersPptx.ResultFileName = "New2";
            //var uploadPptx3 = new WordConvertThread("Convert third 500 PPTX", parametersPptx);
        }