예제 #1
0
        public async Task ConvertFromDoc(CloudConverter cloudConverter, FileInfo fileinfo)
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(ConfigHelper.GetConfigSetting("BaseAddress"));
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www.form-urlencoded"));
                    StringContent content = new StringContent(JsonConvert.SerializeObject(cloudConverter), Encoding.UTF8, "application/json");
                    Debug.WriteLine(fileinfo.Name + " is start to converting.");
                    Console.WriteLine(fileinfo.Name + " is start to converting.");
                    HttpResponseMessage response = client.PostAsync(client.BaseAddress, content).Result;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        System.Uri url        = response.RequestMessage.RequestUri;
                        string     fileprefix = GetFilenamePrefix(fileinfo);
                        if (!Directory.Exists(ConfigHelper.GetConfigSetting("TempFile") + fileprefix))
                        {
                            DirectoryInfo diTemp = CreateDirectorywithfolder("TempFile", fileprefix, FolderType.folderwithprefix);
                            diTempoutput = CreateDirectorywithfolder("TempFile", fileprefix, FolderType.folderwithoutput);
                        }
                        else if (!Directory.Exists(ConfigHelper.GetConfigSetting("TempFile") + fileprefix + @"\Output"))
                        {
                            diTempoutput = CreateDirectorywithfolder("TempFile", fileprefix, FolderType.folderwithoutput);
                        }
                        string fileName = ConfigHelper.GetConfigSetting("TempFile") + fileprefix + @"\Output\"
                                          + Path.GetFileNameWithoutExtension(cloudConverter.filename) + ".pdf";
                        string[] InputFiles = Directory.GetFiles(ConfigHelper.GetConfigSetting("InputFile"));

                        foreach (string file in InputFiles)
                        {
                            if (file.Contains("inprogress"))
                            {
                                File.Delete(file);
                                break;
                            }
                        }
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                        using (WebResponse responsestream = (HttpWebResponse)request.GetResponse())
                        {
                            using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                            {
                                byte[] bytes = ReadFully(responsestream.GetResponseStream());
                                stream.Write(bytes, 0, bytes.Length);
                            }
                        }

                        Console.WriteLine(fileinfo.Name + " is converted.");
                    }
                    else
                    {
                        Console.WriteLine(fileinfo.Name + " is fail to convert.");
                        string fileprefix = GetFilenamePrefix(fileinfo);
                        int    Count      = GetErrorCount(fileprefix);
                        string filename   = cloudConverter.filename;

                        filename = fileprefix + ".inprogress." + Count + ".txt";
                        if (!File.Exists(ConfigHelper.GetConfigSetting("InputFile") + filename))
                        {
                            int Counter = Count + 1;
                            filename = fileprefix + ".inprogress." + Counter + ".txt";
                            using (var myFile = File.Create(ConfigHelper.GetConfigSetting("InputFile") + filename))
                            {
                                // interact with myFile here, it will be disposed automatically
                            }
                        }
                        else
                        {
                            Count = Count + 1;
                            if (Count > 3)
                            {
                                string[] Templfiles = Directory.GetFiles(ConfigHelper.GetConfigSetting("TempFile") + fileprefix);
                                foreach (string file in Templfiles)
                                {
                                    FileInfo Processedfile = new FileInfo(file);
                                    File.Move(file, ConfigHelper.GetConfigSetting("ProcessErrorFile") + Processedfile.Name);
                                }

                                //Getting Inbound Directory to coonvert file from Inbound
                                DirectoryInfo InboundDirectory    = new DirectoryInfo(ConfigHelper.GetConfigSetting("InputFile"));
                                List <string> InputFilesToProcess = GetDirectoryFiles(InboundDirectory, DirectoryFileType.InboundFileWithPrefix, fileprefix);
                                foreach (string item in InputFilesToProcess)
                                {
                                    FileInfo Processedfile = new FileInfo(item);
                                    File.Delete(item);
                                }
                            }
                            File.Delete(ConfigHelper.GetConfigSetting("InputFile") + fileprefix + ".inprogress." + (Count - 1) + ".txt");
                            using (var myFile = File.Create(ConfigHelper.GetConfigSetting("InputFile") + fileprefix + ".inprogress." + (Count) + ".txt"))
                            {
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AppDomain currentDomain                     = AppDomain.CurrentDomain;
                UnhandledExceptionEventArgs arges           = new UnhandledExceptionEventArgs(ex, false);
                ExceptionHelper             exceptionHelper = new ExceptionHelper();
                ExceptionHelper.LogWebException(null, arges);
            }
        }