Пример #1
0
        protected string GetAttachmentFile(string attachmentFile, string originalFile, UserApiConfiguration user)
        {
            var filePathHelper = new FilePathHelper(_configuration, user.Name);

            string localAttachmentFolder = filePathHelper.GetAttachmentsFilesFolder();
            string localAttachmentFile   = $@"{localAttachmentFolder}\{attachmentFile}";

            if (File.Exists(localAttachmentFile))
            {
                return(localAttachmentFile);
            }

            string ftpAttachmentFile = $@"{user.AttachmentsFolder}/{attachmentFile}";

            var ftpHelper = user.Ftp.GetFtpHelper(_logger);

            ftpHelper.DownloadFile(ftpAttachmentFile, localAttachmentFile);

            if (File.Exists(localAttachmentFile))
            {
                ftpHelper.DeleteFile(ftpAttachmentFile);
                return(localAttachmentFile);
            }

            string zipAttachments      = $@"{user.AttachmentsFolder}/{Path.GetFileNameWithoutExtension(originalFile)}.zip";
            string localZipAttachments = $@"{localAttachmentFolder}\{Path.GetFileNameWithoutExtension(originalFile)}.zip";

            // TODO: add retries.
            ftpHelper.DownloadFile(zipAttachments, localZipAttachments);

            if (File.Exists(localZipAttachments))
            {
                using (ZipArchive zipArchive = ZipFile.OpenRead(localZipAttachments))
                {
                    foreach (ZipArchiveEntry entry in zipArchive.Entries)
                    {
                        string entryFileName = $@"{localAttachmentFolder}\{entry.Name}";
                        entry.ExtractToFile(entryFileName, true);
                    }
                }
                ftpHelper.DeleteFile(zipAttachments);
                File.Delete(localZipAttachments); //TODO add retries.
            }

            if (File.Exists(localAttachmentFile))
            {
                return(localAttachmentFile);
            }

            return(null);
        }