Example #1
0
        /// <summary>
        /// Проверка, подходит ли файл для передачи с помощью текущей стратегии добавления
        /// </summary>
        /// <param name="filename">Имя файла</param>
        /// <param name="creationTime">Дата создания файла</param>
        /// <returns>true - подходит для передачи, false - не подходит</returns>
        public bool FileVerification(string filename, DateTime creationTime)
        {
            this.output = ControllerOfOutput.Instance;

            if (this.useCreationDate)
            {
                DateTime cCreatTime    = this.CutDateTime(creationTime.ToUniversalTime());
                DateTime cWorkDateTime = this.CutDateTime(this.GetUtcWorkDate());
                if (cCreatTime != cWorkDateTime)
                {
                    output.WriteProgress(
                        String.Format("Файл {0} не передавался. Не подходящая дата создания {1}. Искали с датой {2}",
                                      filename, cCreatTime.ToString("yyyy:MM:dd"), cWorkDateTime.ToString("yyyy:MM:dd")));
                    return(false);
                }
            }

            if (this.fileNameUsage == "Masked")
            {
                bool regRes = RegexpParsing.FilenameVerification(filename, this.CalculatedFileMask);
                if (!regRes)
                {
                    output.WriteAverageMessage(String.Format("Файл {0} не подходит под маску {1}.",
                                                             filename, this.CalculatedFileMask));
                }
                return(regRes);
            }

            return(true);
        }
Example #2
0
        public override void ActionAsDestination()
        {
            ControllerOfOutput output = ControllerOfOutput.Instance;
            DirectoryInfo      di     = new DirectoryInfo(SettingsClass.Instance.GetTmpDirName());

            //int localErrorCount = 0;

            foreach (FileInfo file in di.GetFiles())
            {
                string ftpUriStr = "";
                try
                {
                    ftpUriStr = @"ftp://" + this.ftpUri + "/" + file.Name;

                    FtpWebRequest request = WebRequest.Create(new Uri(string.Format(ftpUriStr))) as FtpWebRequest;
                    request.Method = WebRequestMethods.Ftp.UploadFile;
                    request        = this.FtpSet(request);

                    //здесь непосредственно передача
                    int totalReadBytesCount = 0;
                    using (FileStream inputStream = File.OpenRead(file.FullName))
                        using (Stream outputStream = request.GetRequestStream())
                        {
                            byte[] buffer        = new byte[16384];
                            int    prevByteCount = 0;
                            int    readBytesCount;
                            while ((readBytesCount = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                outputStream.Write(buffer, 0, readBytesCount);
                                totalReadBytesCount += readBytesCount;
                                double progress    = totalReadBytesCount * 100.0 / inputStream.Length;
                                double totalKBytes = Math.Round((double)totalReadBytesCount / 1000, 0);
                                double allKBytes   = Math.Round((double)inputStream.Length / 1000, 0);

                                prevByteCount = totalReadBytesCount;
                                output.WriteProgress(String.Format("Progress: {0}% {1} Кб из {2} Кб.",
                                                                   Math.Round(progress, 2).ToString("##0.00"), totalKBytes, allKBytes));
                            }
                        }

                    this.statistics.incrementFiles(1);
                    this.statistics.incrementBytesWithCut(file.Length);
                    this.statistics.addResult(WorkResult.Success);
                }
                catch (Exception ex)
                {
                    //сперва проверяем, не вылезла ли ошибка только потому, что такой файл уже существует
                    FtpWebRequest request = WebRequest.Create(new Uri(string.Format(ftpUriStr))) as FtpWebRequest;
                    request.Method = WebRequestMethods.Ftp.GetFileSize;
                    request        = this.FtpSet(request);
                    try
                    {
                        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                    }
                    catch (WebException wex)
                    {
                        FtpWebResponse response = (FtpWebResponse)wex.Response;
                        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                        {
                            string errMsg2 = String.Format("Файл {0} уже существует на ftp-сервере {1}.", file.FullName, ftpUriStr);
                            output.WriteAverageMessage(errMsg2);
                            this.statistics.addMessage(errMsg2);
                            continue;
                        }
                    }

                    string errMsg = String.Format(
                        "Ошибка при передачи файла {0} На сервер {1}. Текст ошибки: {2}", file.Name, ftpUriStr, ex.Message);
                    output.WriteErrors(errMsg);
                    this.statistics.addError(errMsg);
                    return;
                    //localErrorCount++;

                    /*
                     * if (localErrorCount > this.errorCount)
                     * {
                     *  throw new FileExchangeException(errMsg);
                     * }
                     * Thread.Sleep(this.sleepBeforeNext);
                     */
                }
            }
        }