예제 #1
0
        public bool LoadPersonsFromFile(String filename, FileHandler.FileType fileType)
        {
            FileHandler inputFileHandler = FileHandlerFactory.GetFileHandler(fileType);

            Persons = inputFileHandler.Load(filename);
            return(Persons != null);
        }
예제 #2
0
        private void testJsonFile()
        {
            string dataDir = "data";
            //string fileName = dataDir + "\\test.json";
            string          fileName = Server.MapPath(dataDir) + "\\test.json";
            DataManager     dm       = new DataManager();
            List <DataInfo> lsData   = dm.GetDataListByDataSetId(20, null);

            FileHandlerFactory.getFileHandler(FileType.FILETYPE_JSON).WriteToFile(lsData, fileName);

            StreamReader reader  = new StreamReader(fileName);
            string       content = reader.ReadToEnd();

            content = "geodata=" + Server.UrlEncode(content) + "&vcol=MED_AGE";
            reader.Close();
            byte[]         byteArray = Encoding.UTF8.GetBytes(content);
            HttpWebRequest req       = (HttpWebRequest)WebRequest.Create("http://131.94.133.223/py_scripts/auto_corre_cgi.py");

            req.Method      = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            using (Stream dataStream = req.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            string          result;

            using (StreamReader r = new StreamReader(res.GetResponseStream()))
            {
                result = r.ReadToEnd();
            }
        }
예제 #3
0
        public void GetStrategyHandlerTypeReturnTrue()
        {
            var handler  = new FileHandlerFactory();
            var strategy = handler.GetInfoFile(@"/resources/2.txt");

            Assert.IsType <TxtFileStrategy>(strategy);
        }
예제 #4
0
        public static AbstractServer GetServer(ServerType serverType, string port)
        {
            AbstractServer         server         = null;
            AbstractHandlerFactory handlerFactory = null;
            AbstractCommandHandler handler        = null;

            switch (serverType)
            {
            case ServerType.TCPServer:
                server         = new TCPServer(port);
                handlerFactory = new TCPHandlerFactory();
                break;

            case ServerType.FileServer:
                port           = GetFilserverPort(port);
                server         = new FileServer(300, port);
                handlerFactory = new FileHandlerFactory();
                break;

            case ServerType.SyncFileServer:
                port           = GetFilserverPort(port);
                server         = new SyncFileServer(300, port);
                handlerFactory = new FileHandlerFactory();
                break;
            }

            server.commandHandler = handlerFactory.GetKeyBoardRequestHandler();
            handler = server.commandHandler.SetNextHandler(handlerFactory.GetMouseRequestHandler());
            handler = handler.SetNextHandler(handlerFactory.GetKeyLoggerRequestHandler());
            handler = handler.SetNextHandler(handlerFactory.GetScreenRequestHandler());
            handler.SetNextHandler(handlerFactory.GetDefaultRequestHandler());
            return(server);
        }
        public void FileUploadValidationFactory_Validate_ReturnsInvalidOnNullObject()
        {
            var mv        = new List <IFileHandler>();
            var mvFactory = new FileHandlerFactory(mv);

            mvFactory.Validate(null, null).ShouldBe(FileStatusCode.Invalid);
        }
        public void TestFileHandlerFactoryJson()
        {
            FileHandler handler = FileHandlerFactory.GetFileHandler(FileHandler.FileType.JSON);

            Assert.IsNotNull(handler);
            Assert.AreEqual(handler.GetType(), typeof(JsonStrategy));
            List <Person> personList = handler.Load("../../../TestData/PersonTestSet_01.json");

            Assert.IsNotNull(personList);
            Assert.AreEqual(personList.Count, 6);
        }
        public void TestFileHandlerFactoryXml()
        {
            FileHandler handler = FileHandlerFactory.GetFileHandler(FileHandler.FileType.XML);

            Assert.IsNotNull(handler);
            Assert.AreEqual(handler.GetType(), typeof(XmlStrategy));
            List <Person> personList = handler.Load("../../../TestData/PersonTestSet_11.xml");

            Assert.IsNotNull(personList);
            Assert.AreEqual(personList.Count, 9);
        }
예제 #8
0
        public List <MatchResult> FindMatchesFromFile(String filename, FileHandler.FileType fileType)
        {
            FileHandler inputFileHandler = FileHandlerFactory.GetFileHandler(fileType);

            Persons = inputFileHandler.Load(filename);
            if (Persons != null)
            {
                Matches = FindAllMatches();
            }
            Logger.Info($"Found {Matches.Count} matches");
            return(Matches);
        }
예제 #9
0
        private static void TestLaunchFiles(string path)
        {
            var officeFiles = new string[] { "a.doc", "b.docx", "c.xls", "d.xlsx", "e.pptx" };

            // set and launch files
            foreach (var fileName in officeFiles)
            {
                string      filePath = Path.Combine(path, fileName);
                FileHandler handler  = new FileHandlerFactory().Create(fileName);
                handler.LaunchFile(filePath);
            }
        }
예제 #10
0
        public void ConvertToPdf()
        {
            var startTime = DateTime.Now;
            var files     = GetFilesFromInputSource(InfoConstants.AllButPdf);

            TotalNonPdfCount = files.Count;

            if (TotalNonPdfCount.IsFileCountExceeded(InfoConstants.AllButPdf))
            {
                return;
            }
            FileLogger.SetLog(string.Format(InfoConstants.StartingFileConversion, files.Count));

            SetTimer();

            Parallel.For(0, TotalNonPdfCount, index =>
            {
                try
                {
                    var source = files[index];
                    var ext    = Path.GetExtension(source);

                    if (!string.IsNullOrWhiteSpace(ext))
                    {
                        var destination = source.Replace(ext, InfoConstants.PdfExtension);

                        var fileHandler = FileHandlerFactory.GetFileHandler(source);
                        fileHandler.ConvertToPdf(source, destination);
                        AppInstanceModel.Instance.AllInputPdfFiles.Add(destination);

                        Interlocked.Increment(ref TotalNonPdfProcessed);
                        Console.WriteLine(InfoConstants.LogInParallelFor, index, TotalNonPdfProcessed);
                    }
                }
                catch (Exception ex)
                {
                    // Todo: will handle later
                    ErrorMessage.Enqueue(ex.Message);
                }
            });

            FileLogger.SetLog(string.Format(InfoConstants.FileConvertedToPdf, TotalNonPdfProcessed, TotalNonPdfCount, Percent.ToString("F")));
            FileLogger.SetLog(string.Format(InfoConstants.CompleteFileConversion));

            ClearTimer();

            var stopTime = DateTime.Now;

            CalculateTimeRequired(startTime, stopTime);

            SetSummary(InfoConstants.NonPdf, TotalNonPdfCount, TotalNonPdfProcessed);
        }
예제 #11
0
        public bool SaveMatchesToFile(String filename, FileHandler.FileType fileType)
        {
            FileHandler outputFileHandler = FileHandlerFactory.GetFileHandler(fileType);

            if (Matches.Count > 0)
            {
                return(outputFileHandler.Save(filename, Matches));
            }
            else
            {
                Logger.Warn("No matches available to export");
                return(false);
            }
        }
예제 #12
0
        private static void TestCreateFiles(string path)
        {
            var officeFiles = new string[] { "aa.doc", "bb.docx", "cc.xls", "dd.xlsx", "ee.pptx" };

            foreach (var fileName in officeFiles)
            {
                string      filePath = Path.Combine(path, fileName);
                FileHandler handler  = new FileHandlerFactory().Create(fileName);
                handler.CreateFile(filePath, new DocumentModel()
                {
                    Title       = "title",
                    Description = "The more I lean,\n the more I realize \n how much I don't know."
                });
            }
        }
예제 #13
0
파일: Program.cs 프로젝트: Cairns/CCGTest
        private static void WriteFile(string fileName)
        {
            try
            {
                var fileType    = ParseWriteFileName(fileName);
                var fileHandler = FileHandlerFactory <Contact> .Create(fileType);

                fileHandler.Write(contacts, fileName);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                Pause(2);
            }
        }
예제 #14
0
        public void test_01_create_excel_writer()
        {
            //wtf is dit uberhaupt?
            TemplateExcel b         = null;
            String        exception = "";

            try
            {
                b = FileHandlerFactory <TemplateExcel> .create("TemplateExcel");
            }catch (Exception e) {
                exception = e.ToString();
            }
            Assert.AreEqual("", exception);
            Assert.IsTrue(b != null);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Plese enter a value: ");
            Console.WriteLine("Photo: 'a' ");
            Console.WriteLine("PDF: 'b' ");
            Console.WriteLine("XML: 'c' ");

            var value = Console.ReadLine();

            var fileHandler = new FileHandlerFactory(value);
            var creator     = fileHandler.CreateDesiredObject();

            creator.upload();
            creator.download();
        }
예제 #16
0
        private void readAscFile()
        {
            string dataDir  = "upload";
            string fileName = Server.MapPath(dataDir) + "\\crime_mb.asc";

            List <DataInfo> lsData = FileHandlerFactory.getFileHandler(FileType.FILETYPE_ASC).ReadFromFile(fileName, 0, 10000);

            if (lsData.Count > 0)
            {
                DataSetManager dsMgr = new DataSetManager();
                DataSetInfo    ds    = dsMgr.GetDataSetInfoByData(lsData[0], "Jin", "crime_mb_test", "test");
                dsMgr.InsertDataSet(ds);
                DataManager dataMgr = new DataManager();
                dataMgr.CreateDataTable(ds);
                dataMgr.InsertDataList(lsData, ds);
            }
        }
        private static void Main(string[] args)
        {
            var settingsProvider = new SettingsProvider();
            var nameHelper       = new FileNameHelper(settingsProvider);
            var pdfService       = new PdfService(settingsProvider, LogManager.GetLogger(nameof(PdfService)));
            var watcherFactory   = new FileHandlerFactory(nameHelper, pdfService);

            HostFactory.Run(x =>
            {
                x.Service(() => new FileHandlerService(watcherFactory, settingsProvider, LogManager.GetLogger(nameof(FileHandlerService))));
                x.SetServiceName("WindowsService.FileHandlerService");
                x.SetDisplayName("File Handler Service");
                x.StartAutomaticallyDelayed();
                x.RunAsLocalService();
                x.EnableServiceRecovery(y => y.RestartService(1).RestartService(1));
            });
        }
예제 #18
0
        private static void Main(string[] args)
        {
            var settingsProvider = new SettingsProvider();
            var nameHelper       = new FileNameHelper(settingsProvider);
            var pdfService       = new PdfService(settingsProvider, LogManager.GetLogger(nameof(PdfService)));
            var watcherFactory   = new FileHandlerFactory(nameHelper, pdfService);
            var msmqFactory      = new MessageQueueFactory();
            var logger           = LogManager.GetLogger(nameof(FileHandlerService));

            HostFactory.Run(x =>
            {
                x.Service(() => new FileHandlerService(watcherFactory, msmqFactory, settingsProvider, logger));
                x.SetServiceName("MSMQ.StreamScanning.Worker");
                x.SetDisplayName("MSMQ StreamScanning Worker");
                x.StartAutomaticallyDelayed();
                x.RunAsLocalService();
                x.EnableServiceRecovery(y => y.RestartService(1).RestartService(1));
            });
        }
예제 #19
0
        private static void InitializeClasses()
        {
            // Create Logging File Handler
            loggingFileHandler = FileHandlerFactory.CreateLoggingFileHandler();

            // Create Logger
            logger = null;

            if (loggingFileHandler.IsLoggingEnabledFlag())
            {
                logger = LoggerFactory.CreateLogger();
            }
            else // Create fake logger to disable any logging
            {
                logger = LoggerFactory.CreateFakeLogger();
            }

            // Create Planet Config File Handler
            fileHandler = FileHandlerFactory.CreateFileHandler(logger);
        }
        public void FileUploadValidationFactory_IsSupportedExtension_ReturnsTrueAndFalse()
        {
            var mv = new List <IFileHandler>();

            var mvFactory = new FileHandlerFactory(mv);
            //EmptyCollection
            var fileExtension = "ttt";

            mvFactory.IsSupportedExtension(fileExtension).ShouldBeFalse();

            var mvMock   = new Mock <IFileHandler>();
            var mvResult = fileExtension + "ert";

            mvMock.Setup(m => m.SupportedExtensions).Returns(() => new[] { mvResult });
            mv.Add(mvMock.Object);
            //no suitable validator
            mvFactory.IsSupportedExtension(fileExtension).ShouldBeFalse();

            //SuitableVlidator
            mvResult = fileExtension;
            mvFactory.IsSupportedExtension(fileExtension).ShouldBeTrue();
        }
        public void FileUploadValidationFactory_Validate_HasNoSupportForExtension_ReturnNotSupported()
        {
            const string fileExtension = "ttt";
            var          mvMock        = new Mock <IFileHandler>();
            var          mvExtension   = fileExtension + "eee";

            mvMock.Setup(m => m.SupportedExtensions).Returns(() => new[] { mvExtension });

            var mv = new List <IFileHandler> {
                mvMock.Object
            };

            var mvFactory = new FileHandlerFactory(mv);

            var mediaUploadRequest = new FileUploadRequest
            {
                FileName = "ttt." + fileExtension,
                Bytes    = Encoding.UTF8.GetBytes("abcd"),
            };

            mvFactory.Validate(mediaUploadRequest.Extension, mediaUploadRequest.Bytes).ShouldBe(FileStatusCode.Unsupported);
        }
        public void FileUploadValidationFactory_Validate_ReturnsValueFromValidator()
        {
            const string fileExtension = "ttt";
            var          mvMock        = new Mock <IFileHandler>();

            mvMock.Setup(m => m.SupportedExtensions).Returns(() => new[] { fileExtension });
            var result = FileStatusCode.Blocked;

            mvMock.Setup(m => m.Validate(It.IsAny <byte[]>(), It.IsAny <string>(), It.IsAny <object>()))
            .Returns(result);
            var mv = new List <IFileHandler> {
                mvMock.Object
            };

            var mvFactory = new FileHandlerFactory(mv);

            var mediaUploadRequest = new FileUploadRequest
            {
                FileName = "ttt." + fileExtension,
                Bytes    = Encoding.UTF8.GetBytes("abcd"),
            };

            mvFactory.Validate(mediaUploadRequest.Extension, mediaUploadRequest.Bytes).ShouldBe(result);
        }
 public PlayerViewModel(FileHandlerFactory fileHandlerFactory)
 {
     _fileHandlerFactory = fileHandlerFactory;
 }
예제 #24
0
        public void ProcessRequest(HttpContext context)
        {
            HttpRequest  request  = context.Request;
            HttpResponse response = context.Response;
            string       browser  = request.Browser.Browser;

            string   filePath       = "upload";
            string   uploadFileName = null;
            FileType fileType       = FileType.FILETYPE_DEFAULT;

            if (context.Request.RequestType == "GET")
            {
                string message = "Not a file";
                GetResponse(response, message);
            }
            else if (context.Request.RequestType == "POST")
            {
                HttpFileCollection uploadFiles = context.Request.Files;
                string             dsName      = context.Request["datasetName"];
                string             dsCreator   = context.Request["author"];
                string             dsDesc      = context.Request["datasetDesc"];

                string[] jsonResponse = new string[uploadFiles.Count];
                int      i;
                if (browser.Equals("IE"))
                {
                    i            = 1;
                    jsonResponse = new string[uploadFiles.Count - 1];
                }
                else
                {
                    i            = 0;
                    jsonResponse = new string[uploadFiles.Count];
                }
                for (int responseIndex = 0; i < uploadFiles.Count; i++, responseIndex++)
                {
                    HttpPostedFile postedFile = uploadFiles[i];
                    System.Diagnostics.Debug.Write(i + " fileName: " + postedFile.FileName);
                    string fileName = Path.GetFileName(postedFile.FileName);

                    string path = context.Server.MapPath(filePath) + "\\" + fileName;

                    postedFile.SaveAs(path);
                    jsonResponse[responseIndex] = "{\"file\":\"" + filePath.Replace("\\", "\\\\") + "\\\\" + fileName + "\"," +
                                                  "\"name\":\"" + fileName + "\"," +
                                                  "\"type\":\"" + fileName.Substring(fileName.IndexOf('.') + 1) + "\"," +
                                                  "\"size\":" + postedFile.ContentLength + "}";

                    if (path.EndsWith(".shp"))
                    {
                        uploadFileName = path;
                        fileType       = FileType.FILETYPE_SHP;
                    }
                    else if (path.EndsWith(".csv"))
                    {
                        uploadFileName = path;
                        fileType       = FileType.FILETYPE_CSV;
                    }
                    else if (path.EndsWith(".asc"))
                    {
                        uploadFileName = path;
                        fileType       = FileType.FILETYPE_ASC;
                    }
                    else if (path.EndsWith(".default"))
                    {
                        uploadFileName = path;
                        fileType       = FileType.FILETYPE_DEFAULT;
                    }
                }

                // the following code is for read DataInfo list from file and insert into database
                // to avoid being out of memory, I read 100,000 records to insert into database per time
                // if the data is less than 100,000, I read all records in one time
                int  recordsPerTime   = 100000;
                bool isDatasetCreated = false;

                DataSetManager  dsMgr       = new DataSetManager();
                DataManager     dataMgr     = new DataManager();
                DataSetInfo     dsInfo      = null;
                List <DataInfo> lsData      = null;
                int             recordCount = 0;
                while (true)
                {
                    // read DataInfo from file
                    lsData = FileHandlerFactory.getFileHandler(fileType).ReadFromFile(uploadFileName, recordCount, recordsPerTime);

                    // create the dataset, if it is the first time to read and the dataset is not yet created
                    if (!isDatasetCreated)
                    {
                        // create dataset
                        dsInfo = dsMgr.GetDataSetInfoByData(lsData[0], dsCreator, dsName, dsDesc);
                        dsMgr.InsertDataSet(dsInfo);

                        // create data table and insert data list
                        dataMgr.CreateDataTable(dsInfo);

                        isDatasetCreated = true;
                    }
                    dataMgr.InsertDataList(lsData, dsInfo);

                    // the file is read to end if the count of read record is less than record number per time
                    // then stop
                    if (lsData.Count < recordsPerTime)
                    {
                        break;
                    }
                }
                if (browser.Equals("IE"))
                {
                    response.ContentType = "text/html";
                    response.Write("<textarea>[" + String.Join(",", jsonResponse) + "]</textarea>");
                }
                else
                {
                    response.ContentType = "application/json";
                    response.Write("[" + String.Join(",", jsonResponse) + "]");
                }
            }
        }
예제 #25
0
        public async Task <IActionResult> Upload(IFormFile file)
        {
            var rootPath         = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\tempfiles");
            var uploadedFileName = Path.GetFileName(file.FileName);
            var tempFileId       = Guid.NewGuid() + Path.GetExtension(file.FileName);
            var path             = Path.Combine(rootPath, tempFileId);

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                await file.CopyToAsync(stream);
            }

            TransactionModel transactionModel;

            if (System.IO.File.Exists(path))
            {
                var fileHandler = FileHandlerFactory.GetFileHandler(path);
                transactionModel = fileHandler.GetTranscations(path, uploadedFileName);
            }
            else
            {
                ViewBag.Message = LogMessageConstants.FileUploadFailed;
                ViewBag.Success = false;
                return(View("Index"));
            }

            try
            {
                System.IO.File.Delete(path);
                Log.Information(LogMessageConstants.DeleteFileSuccess);
            }
            catch (Exception e)
            {
                Log.Information(LogMessageConstants.DeleteFileError + e.Message);
            }

            if (transactionModel == null)
            {
                throw new Exception(LogMessageConstants.ErrorProcessingFile);
            }

            if (transactionModel.Errors.Any())
            {
                var errorMsg = string.Join("\n", transactionModel.Errors);
                ViewBag.Message = errorMsg;
                ViewBag.Success = false;
                return(View("Index"));
            }

            var isSaved = TransactionService.AddTransactions(transactionModel.Transcations);

            if (isSaved)
            {
                ViewBag.Message = LogMessageConstants.FileUploadSuccessful;
                ViewBag.Success = true;
                return(View("Index"));
            }
            ViewBag.Message = LogMessageConstants.FileUploadFailed;
            ViewBag.Success = false;
            return(View("Index"));
        }
        public void FileUploadValidationFactory_IsSupportedExtension_Throws()
        {
            var mv = new FileHandlerFactory(null);

            Should.Throw <ArgumentNullException>(() => mv.IsSupportedExtension("ttt").ShouldBeFalse());
        }
        public async Task <IActionResult> Upload(IFormFile file)
        {
            Logger.Log.Debug(LogConstant.FileUploadBeginning);

            var rootPath         = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\files");
            var uploadedFileName = Path.GetFileName(file.FileName);
            var tempFileId       = Guid.NewGuid() + Path.GetExtension(file.FileName);
            var path             = Path.Combine(rootPath, tempFileId);

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                await file.CopyToAsync(stream);

                Logger.Log.Debug(LogConstant.TempFileSaved);
            }

            TransactionModel transactionModel;

            if (System.IO.File.Exists(path))
            {
                var fileHandler = FileHandlerFactory.GetFileHandler(path);
                transactionModel = fileHandler.GetTranscations(path, uploadedFileName);
            }
            else
            {
                Logger.Log.Debug(LogConstant.TempFileNotSaved);
                return(BadRequest(LogConstant.FileUploadFailed));
            }

            try
            {
                System.IO.File.Delete(path);
                Logger.Log.Debug(LogConstant.DeletedTemporarilyFile);
            }
            catch (Exception e)
            {
                Logger.Log.Warn(LogConstant.DeleteFileError + e.Message + e.InnerException);
            }

            if (transactionModel == null)
            {
                throw new Exception(LogConstant.ErrorProcessingFile);
            }
            Logger.Log.Debug(LogConstant.ReceivedTransactionModel);

            if (transactionModel.Errors.Any())
            {
                var errorMsg = string.Join("\n", transactionModel.Errors);
                Logger.Log.Error(errorMsg); // Invalid items
                return(BadRequest(errorMsg));
            }

            var isSaved = TransactionService.AddTransactions(transactionModel.Transcations);

            if (isSaved)
            {
                Logger.Log.Info(LogConstant.FileUploadSuccessful);
                return(Ok(LogConstant.FileUploadSuccessful));
            }

            Logger.Log.Error(LogConstant.FileUploadFailed);
            return(BadRequest(LogConstant.FileUploadFailed));
        }