public Task <int> DownloadUnZip()
        {
            return(Task.Run(() =>
            {
                string filename = ConfigService.DTCFileName;
                string username = ConfigService.DTCUserName;
                string password = ConfigService.DTCPassword;
                String downloadDate = DateTime.Now.ToString("dd.MM.yyyy");
                string destinationFolder = AppDomain.CurrentDomain.BaseDirectory + "dtc/" + downloadDate;
                string destination = destinationFolder + "/dtcinventory.zip";

                int progress = 0;
                double progressP = 0;

                if (!Directory.Exists(destinationFolder))
                {
                    Directory.CreateDirectory(destinationFolder);
                }

                FtpWebRequest frequest = (FtpWebRequest)WebRequest.Create(filename);
                frequest.Method = WebRequestMethods.Ftp.GetFileSize;
                frequest.Credentials = new NetworkCredential(username, password);
                frequest.UsePassive = true;
                frequest.UseBinary = true;
                frequest.KeepAlive = true; //don't close the connection
                int dataLength = (int)frequest.GetResponse().ContentLength;

                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(filename);
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                request.Credentials = new NetworkCredential(username, password);
                request.UseBinary = true;

                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                {
                    using (Stream rs = response.GetResponseStream())
                    {
                        using (FileStream ws = new FileStream(destination, FileMode.Create))
                        {
                            byte[] buffer = new byte[2048];
                            int bytesRead = rs.Read(buffer, 0, buffer.Length);

                            System.Diagnostics.Debug.WriteLine("Downloading {0} ", filename);
                            while (bytesRead > 0)
                            {
                                ws.Write(buffer, 0, bytesRead);
                                bytesRead = rs.Read(buffer, 0, buffer.Length);
                                progress += bytesRead;
                                progressP = (double)progress / dataLength * 100;

                                System.Diagnostics.Debug.WriteLine("\r{0:N2}%   ", progressP);
                            }
                        }
                    }
                }

                System.Diagnostics.Debug.WriteLine("Download completed on {0:HH:mm:ss tt}", DateTime.Now);
                System.Diagnostics.Debug.WriteLine("Extracting file...");

                ZipFile.ExtractToDirectory(destination, destinationFolder);

                System.Diagnostics.Debug.WriteLine("File extracted!  =)");


                AdminImportRequestModel Model = new AdminImportRequestModel();
                Model.FilePath = destinationFolder;
                Model.EntityId = Int32.Parse(ConfigService.AutoDealioEntityID);
                Model.WebsiteId = Int32.Parse(ConfigService.AutoDealioWebsiteID);



                _indexer.parseCSV(Model);
                return 5;
            }));
        }
예제 #2
0
        public Task <int> parseCSV(AdminImportRequestModel model)
        {
            return(Task.Run(() =>
            {
                //Use downloadDate because when the file gets downloaded it is saved by date and then this function runs
                //Directly after the file has been downloaded
                String downloadDate = DateTime.Now.ToString("dd.MM.yyyy");
                string destination = AppDomain.CurrentDomain.BaseDirectory + "dtc/" + downloadDate + "/dtcinventory.txt";
                _AdminImport = model;

                EVA_Entity Entity = _entityService.GetByID(model.EntityId);

                using (var sr = new StreamReader(destination))
                {
                    var csv = new CsvReader(sr);
                    csv.Configuration.Delimiter = "\t";
                    csv.Configuration.IgnoreHeaderWhiteSpace = true;
                    var records = csv.GetRecords <TestDTCDealerRequestModel>();


                    foreach (var record in records)
                    {
                        if (record == null)
                        {
                            break;
                        }
                        //Im making a new instance of RRM which is going to get passed in at the end
                        RecordRequestModel RRM = new RecordRequestModel();
                        RRM.EntityId = this._AdminImport.EntityId;
                        RRM.WebsiteId = this._AdminImport.WebsiteId;
                        RRM.AttributeId = 136;
                        RRM.Values = new List <ValueRequestModel>();
                        RRM.Medias = new List <MediaRequestModel>();

                        TestDTCDealerRequestModel VehicleRM = new TestDTCDealerRequestModel();

                        PropertyInfo[] Y = VehicleRM.GetType().GetProperties();

                        for (var p = 0; p < Y.Length; p++)
                        {
                            string slug = UtilityService.camelCaseToDash(Y[p].Name);

                            foreach (var attribute in Entity.Attributes)
                            {
                                if (slug == attribute.Slug)
                                {
                                    ValueRequestModel ValueRm = new ValueRequestModel();
                                    ValueRm.AttributeId = attribute.ID;
                                    ValueRm.ValueString = record.GetType().GetProperty(Y[p].Name).GetValue(record, null).ToString();
                                    System.Diagnostics.Debug.WriteLine(ValueRm.ValueString);
                                    RRM.Values.Add(ValueRm);
                                }
                            }
                        }

                        //Within the CSV there are numbers seperated by pipes which are the img number
                        //Here we seperate them on the pipe and insert them into the url
                        if (record.Images != null)
                        {
                            string[] RecordMediaArray = record.Images.Split('|');
                            var x = 0;
                            foreach (string recordMediaPath in RecordMediaArray)
                            {
                                RecordMediaRequestModel RecordMedia = new RecordMediaRequestModel();
                                RecordMedia.FileName = "http://img.leaddelivery.net/images/" + record.VIN + "/Original/" + recordMediaPath + ".jpg";
                                RecordMedia.MediaType = "3";
                                RecordMedia.FileType = "image/jpeg";
                                System.Diagnostics.Debug.WriteLine(RecordMedia.FileName);
                                //Setting first image equal to the cover photo
                                if (x == 0)
                                {
                                    RecordMedia.IsCoverPhoto = true;
                                }
                                else
                                {
                                    RecordMedia.IsCoverPhoto = false;
                                }

                                RRM.Medias.Add(RecordMedia);

                                x++;
                            }
                        }
                        //The RRM final gets inserted into the ProcessAsync Task
                        _InsertRecord.ProcessAsync(RRM);
                    }
                }
                return 5;
            }));
        }