예제 #1
0
        public void Copy_Should_Create_Target_Directory_If_Not_Existing()
        {
            // Arrange
            const string sourceFileDirectory = "c:\\source\\sourceFile.txt";
            const string targetFileDirectory = "c:\\copy\\copiedFile.txt";

            var fileSystem = CreateFileSystem();

            /* Target Directory not created ahead of time */

            var sut = new FileCopyService(fileSystem);

            // Act && Assert
            sut.Invoking(x => x.Copy(sourceFileDirectory, targetFileDirectory))
            .Should()
            .NotThrow <DirectoryNotFoundException>();
        }
예제 #2
0
        public void Copy_Should_Copy_Source_File_To_Target_Location()
        {
            // Arrange
            const string sourceFilePath = "c:\\source\\sourceFile.txt";
            const string targetFilePath = "c:\\copy\\copiedFile.txt";

            var fileSystem = CreateFileSystem();

            AddDirectoryToFileSystem(fileSystem, targetFilePath);

            var sut        = new FileCopyService(fileSystem);
            var sourceFile = fileSystem.GetFile(sourceFilePath);

            // Act
            sut.Copy(sourceFilePath, targetFilePath);

            var targetFile = fileSystem.GetFile(targetFilePath);

            // Assert
            targetFile.Should().BeEquivalentTo(sourceFile);
        }
예제 #3
0
        /// <summary>
        /// Run this command operation loop based on this task delay
        /// </summary>
        /// <param name="stoppingToken"></param>
        /// <returns></returns>
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                Log.Information("Worker running at: {time}", DateTimeOffset.Now);

                HashSet <string> filesToCopy = new HashSet <string>(FileCopyService.CheckFiles(_sourcePath, _destinationPath, _fileExtension));

                if (!filesToCopy.Any())
                {
                    Log.Debug($"Nothing to transfer.");
                }
                else
                {
                    filesToCopy.ExceptWith(_inProgressTransfers);
                    _inProgressTransfers.UnionWith(filesToCopy);

                    // if we're "deleting" files AFTER they've been copied, we are effectively performing a Move
                    if (_deleteOnCopy)
                    {
                        FileCopyService.MoveFiles(_destinationPath, filesToCopy);
                    }
                    else
                    {
                        FileCopyService.CopyFiles(_destinationPath, filesToCopy);
                    }

                    // Reset this list for the next iteration
                    _inProgressTransfers.ExceptWith(filesToCopy);

                    int copiedFiles = filesToCopy.Count;
                    Log.Information($"Successfully {(_deleteOnCopy ? "moved" : "copied")} {copiedFiles} file(s).");
                }

                // wait 30 seconds before running again
                await Task.Delay(30000, stoppingToken);
            }
        }
예제 #4
0
        public static void Main(string[] args)
        {
            if (args.Count() != 3)
            {
                Console.WriteLine("Not enough arguments.");
                return;
            }
            string src     = args[0];
            string dest    = args[1];
            int    threads = 0;

            int.TryParse(args[2], out threads);

            if ((!Directory.Exists(src) || !Directory.Exists(Directory.GetDirectoryRoot(dest)) ||
                 src == dest) || threads < 1)
            {
                return;
            }
            Console.WriteLine(threads);
            FileCopyService fileCopyService = new FileCopyService(new TaskQueue(threads));

            fileCopyService.StartCopying(Path.GetFullPath(src), Path.GetFullPath(dest));
        }
예제 #5
0
        public void Copy_Should_Handle_Copying_Multiple_Source_File_Types_To_Target_Location(
            string sourceFilePath,
            string embeddedResource)
        {
            // Arrange
            var targetFilePath = $"c:\\copy\\copiedFile.{Path.GetExtension(sourceFilePath)}";

            var fileSystem = CreateFileSystem();

            AddDirectoryToFileSystem(fileSystem, targetFilePath);
            AddEmbeddedResourceToFileSystem(fileSystem, sourceFilePath, embeddedResource);

            var sut        = new FileCopyService(fileSystem);
            var sourceFile = fileSystem.GetFile(sourceFilePath);

            // Act
            sut.Copy(sourceFilePath, targetFilePath);

            var targetFile = fileSystem.GetFile(targetFilePath);

            // Assert
            targetFile.Should().BeEquivalentTo(sourceFile);
        }
예제 #6
0
        public void Copy_Should_Delete_Existing_Target_File_From_Target_Location()
        {
            // Arrange
            const string sourceFileDirectory        = "c:\\source\\sourceFile.txt";
            const string targetFileDirectory        = "c:\\copy\\copiedFile.txt";
            const string originalTargetFileContents = "Existing target file";

            var fileSystem = CreateFileSystem();

            AddDirectoryToFileSystem(fileSystem, targetFileDirectory);
            fileSystem.AddFile(targetFileDirectory, new MockFileData(originalTargetFileContents));

            var sut        = new FileCopyService(fileSystem);
            var sourceFile = fileSystem.GetFile(sourceFileDirectory);

            // Act
            sut.Copy(sourceFileDirectory, targetFileDirectory);

            var targetFile = fileSystem.GetFile(targetFileDirectory);

            // Assert
            targetFile.TextContents.Should().NotBe(originalTargetFileContents);
            targetFile.Should().BeEquivalentTo(sourceFile);
        }
        public async Task <ActionResult> CsvBackUpStart()
        {
            /*
             * //ファイルを空にする
             * FileEmpty();
             *
             * //ファイルを開く
             * using (System.IO.FileStream read = new System.IO.FileStream(
             *  @"C:\Users\kin_y\Desktop\android-studio-ide-201.7042882-windows.exe",
             *  //@"C:\Users\kin_y\Desktop\testfile2",
             *  //@"C:\Users\kin_y\Desktop\csvData.txt",
             *  System.IO.FileMode.Open,
             *  System.IO.FileAccess.Read))
             * {
             *
             *  //ファイルを一時的に読み込むバイト型配列を作成する
             *  byte[] bs = new byte[0x1000];
             *
             *  //ファイルが存在する場合は、末尾に追加する(ファイルの末尾をシーク)。
             *  //存在しない場合は、新たに作成する。書き込み時にのみ使用できる。
             *  using (System.IO.FileStream write = new System.IO.FileStream(
             *      @"C:\Users\kin_y\Desktop\copy2.exe",
             *      //@"C:\Users\kin_y\Desktop\csvWritten.txt",
             *      System.IO.FileMode.Append,
             *      System.IO.FileAccess.Write,
             *      System.IO.FileShare.ReadWrite))
             *  {
             *      //ファイルをすべて読み込む
             *      //chunk ネットワークに接続するときは必要かも
             *      for (; ; )
             *      {
             *          //ファイルの一部を読み込む
             *          int readSize = read.Read(bs, 0, bs.Length);
             *          //ファイルをすべて読み込んだときは終了する
             *          if (readSize == 0)
             *              break;
             *
             *          //バイト型配列の内容をすべて書き込む
             *          write.Write(bs, 0, bs.Length);
             *
             *      }
             *      //ファイル閉じる
             *      write.Close();
             *  }
             * }
             *
             * //return RedirectToAction("CsvBackUpIndex");
             * return RedirectToAction("CsvBackUpIndex", "CsvBackUp");
             *
             *
             * //// 対象のコレクション
             * //var list = Enumerable.Range(1, 10);
             *
             * //// N 個ずつの N
             * //var chunkSize = 3;
             *
             * //var chunks = list.Select((v, i) => new { v, i })
             * //    .GroupBy(x => x.i / chunkSize)
             * //    .Select(g => g.Select(x => x.v));
             *
             * //// 動作確認
             * //foreach (var chunk in chunks)
             * //{
             * //    foreach (var item in chunk)
             * //        Console.Write($"{item} ");
             * //    Console.WriteLine();
             * //}
             */
            var             srcPath  = @"C:\Users\kin_y\Desktop\android-studio-ide-201.7042882-windows.exe";
            var             destPath = @"C:\Users\kin_y\Desktop\copy2.exe";
            FileCopyService service  = new FileCopyService();
            await service.FileCopyAsync(srcPath, destPath);

            return(new EmptyResult());
        }