private void When_moving_file_it_must_update_properties()
        {
            // Arrange
            const string path = @"c:\some\file.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingTextFile(path, DefaultContents)
                                     .IncludingDirectory(@"c:\other")
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(path);

            string beforeDirectoryName = fileInfo.DirectoryName;

            // Act
            fileInfo.MoveTo(@"c:\other\file.txt");

            // Assert
            beforeDirectoryName.Should().Be(@"c:\some");
            fileInfo.Exists.Should().BeTrue();
            fileInfo.Name.Should().Be("file.txt");
            fileInfo.Extension.Should().Be(".txt");
            fileInfo.DirectoryName.Should().Be(@"c:\other");
            fileInfo.Directory.ShouldNotBeNull().FullName.Should().Be(@"c:\other");
            fileInfo.FullName.Should().Be(@"c:\other\file.txt");
            fileInfo.ToString().Should().Be(@"c:\other\file.txt");
        }
        /// <summary>
        /// Solve the specified situation by using the session, storage, localFile and remoteId.
        /// Moves the local file/folder to the new location.
        /// </summary>
        /// <param name="localFile">Old local file/folder.</param>
        /// <param name="remoteId">Remote identifier.</param>
        /// <param name="localContent">Hint if the local content has been changed.</param>
        /// <param name="remoteContent">Information if the remote content has been changed.</param>
        public override void Solve(
            IFileSystemInfo localFile,
            IObjectId remoteId,
            ContentChangeType localContent  = ContentChangeType.NONE,
            ContentChangeType remoteContent = ContentChangeType.NONE)
        {
            // Move local object
            var    savedObject = this.Storage.GetObjectByRemoteId(remoteId.Id);
            string newPath     = remoteId is IFolder?this.Storage.Matcher.CreateLocalPath(remoteId as IFolder) : this.Storage.Matcher.CreateLocalPath(remoteId as IDocument);

            if (remoteId is IFolder)
            {
                IDirectoryInfo dirInfo = localFile as IDirectoryInfo;
                string         oldPath = dirInfo.FullName;
                if (!dirInfo.FullName.Equals(newPath))
                {
                    dirInfo.MoveTo(newPath);
                    dirInfo.LastWriteTimeUtc = (remoteId as IFolder).LastModificationDate != null ? (DateTime)(remoteId as IFolder).LastModificationDate : dirInfo.LastWriteTimeUtc;
                    OperationsLogger.Info(string.Format("Moved local folder {0} to {1}", oldPath, newPath));
                }
                else
                {
                    return;
                }
            }
            else if (remoteId is IDocument)
            {
                IFileInfo fileInfo = localFile as IFileInfo;
                string    oldPath  = fileInfo.FullName;
                fileInfo.MoveTo(newPath);
                fileInfo.LastWriteTimeUtc = (remoteId as IDocument).LastModificationDate != null ? (DateTime)(remoteId as IDocument).LastModificationDate : fileInfo.LastWriteTimeUtc;
                OperationsLogger.Info(string.Format("Moved local file {0} to {1}", oldPath, newPath));
            }

            savedObject.Name                   = (remoteId as ICmisObject).Name;
            savedObject.ParentId               = remoteId is IFolder ? (remoteId as IFolder).ParentId : (remoteId as IDocument).Parents[0].Id;
            savedObject.LastChangeToken        = (remoteId is IDocument && remoteContent != ContentChangeType.NONE) ? savedObject.LastChangeToken : remoteId is ICmisObject ? (remoteId as ICmisObject).ChangeToken : null;
            savedObject.LastLocalWriteTimeUtc  = localFile.LastWriteTimeUtc;
            savedObject.LastRemoteWriteTimeUtc = (remoteId is IDocument && remoteContent != ContentChangeType.NONE) ? savedObject.LastRemoteWriteTimeUtc : (remoteId as ICmisObject).LastModificationDate;
            savedObject.Ignored                = (remoteId as ICmisObject).AreAllChildrenIgnored();
            this.Storage.SaveMappedObject(savedObject);
            if (remoteId is IDocument && remoteContent != ContentChangeType.NONE)
            {
                throw new ArgumentException("Remote content has also been changed => force crawl sync.");
            }
        }
        private void When_moving_file_it_must_succeed()
        {
            // Arrange
            const string sourcePath      = @"c:\some\file.txt";
            const string destinationPath = @"c:\other\renamed.txt";

            IFileSystem fileSystem = new FakeFileSystemBuilder()
                                     .IncludingTextFile(sourcePath, DefaultContents, attributes: FileAttributes.ReadOnly)
                                     .IncludingDirectory(@"c:\other")
                                     .Build();

            IFileInfo fileInfo = fileSystem.ConstructFileInfo(sourcePath);

            // Act
            fileInfo.MoveTo(destinationPath);

            // Assert
            fileSystem.File.Exists(sourcePath).Should().BeFalse();
            fileSystem.File.ReadAllText(destinationPath).Should().Be(DefaultContents);
            fileSystem.File.GetAttributes(destinationPath).Should().Be(FileAttributes.ReadOnly);
        }
Exemple #4
0
        public override void Solve(
            IFileSystemInfo localFileSystemInfo,
            IObjectId remoteId,
            ContentChangeType localContent,
            ContentChangeType remoteContent)
        {
            var    savedObject = this.Storage.GetObjectByRemoteId(remoteId.Id);
            string newPath     = remoteId is IFolder?this.Storage.Matcher.CreateLocalPath(remoteId as IFolder) : this.Storage.Matcher.CreateLocalPath(remoteId as IDocument);

            if (remoteId is IFolder)
            {
                IDirectoryInfo dirInfo = localFileSystemInfo as IDirectoryInfo;
                string         oldPath = dirInfo.FullName;
                if (!dirInfo.FullName.Equals(newPath))
                {
                    dirInfo.MoveTo(newPath);
                    OperationsLogger.Info(string.Format("Moved local folder {0} to {1}", oldPath, newPath));
                }
                else
                {
                    return;
                }
            }
            else if (remoteId is IDocument)
            {
                IFileInfo fileInfo = localFileSystemInfo as IFileInfo;
                string    oldPath  = fileInfo.FullName;
                fileInfo.MoveTo(newPath);
                OperationsLogger.Info(string.Format("Moved local file {0} to {1}", oldPath, newPath));
            }

            savedObject.Name     = (remoteId as ICmisObject).Name;
            savedObject.Ignored  = (remoteId as ICmisObject).AreAllChildrenIgnored();
            savedObject.ParentId = remoteId is IFolder ? (remoteId as IFolder).ParentId : (remoteId as IDocument).Parents[0].Id;
            this.Storage.SaveMappedObject(savedObject);

            this.changeChangeSolver.Solve(localFileSystemInfo, remoteId, localContent, remoteContent);
        }
Exemple #5
0
        private async Task <Unit> ProcessFile(Guid fileId,
                                              Guid fileProfileId,
                                              String fileName,
                                              CancellationToken cancellationToken)
        {
            IFileInfo   inProgressFile = null;
            FileProfile fileProfile    = null;

            try
            {
                fileProfile = await this.FileProcessorManager.GetFileProfile(fileProfileId, cancellationToken);

                if (fileProfile == null)
                {
                    throw new NotFoundException($"No file profile found with Id {fileProfileId}");
                }

                // Check the processed/failed directories exist
                if (this.FileSystem.Directory.Exists(fileProfile.ProcessedDirectory) == false)
                {
                    Logger.LogWarning($"Creating Directory {fileProfile.ProcessedDirectory} as not found");
                    this.FileSystem.Directory.CreateDirectory(fileProfile.ProcessedDirectory);
                }

                if (this.FileSystem.Directory.Exists(fileProfile.FailedDirectory) == false)
                {
                    Logger.LogWarning($"Creating Directory {fileProfile.FailedDirectory} as not found");
                    this.FileSystem.Directory.CreateDirectory(fileProfile.FailedDirectory);
                }

                inProgressFile = this.FileSystem.FileInfo.FromFileName(fileName);

                if (inProgressFile.Exists == false)
                {
                    throw new FileNotFoundException($"File {inProgressFile.FullName} not found");
                }

                FileAggregate fileAggregate =
                    await this.FileAggregateRepository.GetLatestVersion(fileId, cancellationToken);

                if (fileAggregate.IsCreated == false)
                {
                    throw new InvalidOperationException($"File with Id {fileId} not created");
                }

                String fileContent = null;
                //Open file for Read\Write
                using (Stream fs = inProgressFile.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
                {
                    //Create object of StreamReader by passing FileStream object on which it needs to operates on
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        //Use ReadToEnd method to read all the content from file
                        fileContent = await sr.ReadToEndAsync();
                    }
                }

                if (String.IsNullOrEmpty(fileContent) == false)
                {
                    String[] fileLines = fileContent.Split(fileProfile.LineTerminator);

                    foreach (String fileLine in fileLines)
                    {
                        fileAggregate.AddFileLine(fileLine);
                    }

                    await this.FileAggregateRepository.SaveChanges(fileAggregate, cancellationToken);
                }

                Logger.LogInformation(
                    $"About to move file {inProgressFile.Name} to [{fileProfile.ProcessedDirectory}]");

                // TODO: Move file now
                inProgressFile.MoveTo($"{fileProfile.ProcessedDirectory}/{inProgressFile.Name}");

                return(new Unit());
            }
            catch (Exception e)
            {
                if (inProgressFile != null && fileProfile != null)
                {
                    inProgressFile.MoveTo($"{fileProfile.FailedDirectory}/{inProgressFile.Name}");
                }

                Logger.LogError(e);
                throw;
            }
        }
Exemple #6
0
        // TODO: Create a domain service

        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        /// <exception cref="NotFoundException">No file profile found with Id {request.FileProfileId}</exception>
        /// <exception cref="System.IO.FileNotFoundException">File {file.FullName} not found</exception>
        /// <exception cref="System.IO.DirectoryNotFoundException">Directory {fileProfile.ListeningDirectory} not found</exception>
        public async Task <Guid> Handle(UploadFileRequest request,
                                        CancellationToken cancellationToken)
        {
            DateTime importLogDateTime = request.FileUploadedDateTime;

            // This will now create the import log and add an event for the file being uploaded
            Guid importLogId = Helpers.CalculateFileImportLogAggregateId(importLogDateTime.Date, request.EstateId);

            // Get the import log
            FileImportLogAggregate fileImportLogAggregate = await this.FileImportLogAggregateRepository.GetLatestVersion(importLogId, cancellationToken);

            if (fileImportLogAggregate.IsCreated == false)
            {
                // First file of the day so create
                fileImportLogAggregate.CreateImportLog(request.EstateId, importLogDateTime);
            }

            // Move the file
            FileProfile fileProfile = await this.FileProcessorManager.GetFileProfile(request.FileProfileId, cancellationToken);

            if (fileProfile == null)
            {
                throw new NotFoundException($"No file profile found with Id {request.FileProfileId}");
            }

            // Copy file from the temp location to file processing listening directory
            IFileInfo file = this.FileSystem.FileInfo.FromFileName(request.FilePath);

            if (file.Exists == false)
            {
                throw new FileNotFoundException($"File {file.FullName} not found");
            }
            String originalName = file.Name;

            if (this.FileSystem.Directory.Exists(fileProfile.ListeningDirectory) == false)
            {
                throw new DirectoryNotFoundException($"Directory {fileProfile.ListeningDirectory} not found");
            }

            // Read the file data
            String fileContent = null;

            //Open file for Read\Write
            using (Stream fs = file.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
            {
                //Create object of StreamReader by passing FileStream object on which it needs to operates on
                using (StreamReader sr = new StreamReader(fs))
                {
                    //Use ReadToEnd method to read all the content from file
                    fileContent = await sr.ReadToEndAsync();
                }
            }

            Guid fileId = this.CreateGuidFromFileData(fileContent);

            String fileDestination = $"{fileProfile.ListeningDirectory}//{request.EstateId:N}-{fileId:N}";

            file.MoveTo(fileDestination, overwrite: true);

            // Update Import log aggregate
            fileImportLogAggregate.AddImportedFile(fileId, request.MerchantId, request.UserId, request.FileProfileId, originalName, fileDestination, request.FileUploadedDateTime);

            // Save changes
            await this.FileImportLogAggregateRepository.SaveChanges(fileImportLogAggregate, cancellationToken);

            return(fileId);
        }
Exemple #7
0
 public void MoveTo(string destFileName)
 {
     _inner.MoveTo(destFileName);
 }
Exemple #8
0
        private bool LoadCacheFile(IFileInfo target, IDocument remoteDocument, IFileSystemInfoFactory fsFactory)
        {
            if (this.TransmissionStorage == null)
            {
                return(false);
            }

            IFileTransmissionObject obj = this.TransmissionStorage.GetObjectByRemoteObjectId(remoteDocument.Id);

            if (obj == null)
            {
                return(false);
            }

            IFileInfo localFile = fsFactory.CreateFileInfo(obj.LocalPath);

            if (!localFile.Exists)
            {
                return(false);
            }

            if (obj.LastChangeToken != remoteDocument.ChangeToken || localFile.Length != obj.LastContentSize)
            {
                localFile.Delete();
                return(false);
            }

            try {
                byte[] localHash;
                using (var f = localFile.Open(FileMode.Open, FileAccess.Read, FileShare.None)) {
                    localHash = SHA1Managed.Create().ComputeHash(f);
                }

                if (!localHash.SequenceEqual(obj.LastChecksum))
                {
                    localFile.Delete();
                    return(false);
                }

                if (target.FullName != obj.LocalPath)
                {
                    if (target.Exists)
                    {
                        Guid?uuid = target.Uuid;
                        if (uuid != null)
                        {
                            localFile.Uuid = uuid;
                        }

                        target.Delete();
                    }

                    localFile.MoveTo(target.FullName);
                    target.Refresh();
                }

                return(true);
            } catch (Exception) {
                localFile.Delete();
                return(false);
            }
        }
        private void SyncNamesAndDates(IFileSystemInfo local, IFileableCmisObject remote, IMappedObject mappedObject)
        {
            DateTime?oldRemoteModificationDate = remote.LastModificationDate;
            DateTime oldLocalModificationDate  = local.LastWriteTimeUtc;

            // Sync Names
            if (mappedObject.Name != local.Name && mappedObject.Name == remote.Name)
            {
                // local has been renamed => rename remote
                remote.Rename(local.Name, true);
                mappedObject.Name = local.Name;
            }
            else if (mappedObject.Name == local.Name && mappedObject.Name != remote.Name)
            {
                // remote has been renamed => rename local
                if (local is IFileInfo)
                {
                    IFileInfo localFile = local as IFileInfo;
                    localFile.MoveTo(Path.Combine(localFile.Directory.FullName, remote.Name));
                }
                else if (local is IDirectoryInfo)
                {
                    IDirectoryInfo localFolder = local as IDirectoryInfo;
                    localFolder.MoveTo(Path.Combine(localFolder.Parent.FullName, remote.Name));
                }
                else
                {
                    throw new ArgumentException("Solved move conflict => invoke crawl sync to detect other changes");
                }

                mappedObject.Name = remote.Name;
            }
            else if (mappedObject.Name != local.Name && mappedObject.Name != remote.Name)
            {
                // both are renamed => rename to the latest change
                DateTime localModification  = local.LastWriteTimeUtc;
                DateTime remoteModification = (DateTime)remote.LastModificationDate;
                if (localModification > remoteModification)
                {
                    // local modification is newer
                    remote.Rename(local.Name, true);
                    mappedObject.Name = local.Name;
                }
                else
                {
                    // remote modification is newer
                    if (local is IFileInfo)
                    {
                        IFileInfo localFile = local as IFileInfo;
                        localFile.MoveTo(Path.Combine(localFile.Directory.FullName, remote.Name));
                    }
                    else if (local is IDirectoryInfo)
                    {
                        IDirectoryInfo localFolder = local as IDirectoryInfo;
                        localFolder.MoveTo(Path.Combine(localFolder.Parent.FullName, remote.Name));
                    }
                    else
                    {
                        throw new ArgumentException("Solved move conflict => invoke crawl sync to detect other changes");
                    }

                    local.LastWriteTimeUtc = (DateTime)remote.LastModificationDate;
                    mappedObject.Name      = remote.Name;
                }
            }

            // Sync modification dates
            if (oldRemoteModificationDate != null)
            {
                if (oldLocalModificationDate > oldRemoteModificationDate && this.ServerCanModifyDateTimes)
                {
                    remote.UpdateLastWriteTimeUtc(oldLocalModificationDate);
                    local.LastWriteTimeUtc = oldLocalModificationDate;
                }
                else if (oldLocalModificationDate < (DateTime)oldRemoteModificationDate)
                {
                    local.LastWriteTimeUtc = (DateTime)oldRemoteModificationDate;
                }
            }

            mappedObject.LastLocalWriteTimeUtc  = local.LastWriteTimeUtc;
            mappedObject.LastRemoteWriteTimeUtc = (DateTime)remote.LastModificationDate;
            mappedObject.ParentId        = remote.Parents[0].Id;
            mappedObject.LastChangeToken = remote.ChangeToken;
            mappedObject.Ignored         = remote.AreAllChildrenIgnored();
            this.Storage.SaveMappedObject(mappedObject);
        }
Exemple #10
0
        /// <summary>
        /// Renames the specified localFile to the name of the given remoteId object by using the storage, localFile and remoteId.
        /// </summary>
        /// <param name="localFile">Local file or folder. It is the source file/folder reference, which should be renamed.</param>
        /// <param name="remoteId">Remote identifier. Should be an instance of IFolder or IDocument.</param>
        /// <param name="localContent">Hint if the local content has been changed.</param>
        /// <param name="remoteContent">Information if the remote content has been changed.</param>
        public override void Solve(
            IFileSystemInfo localFile,
            IObjectId remoteId,
            ContentChangeType localContent  = ContentChangeType.NONE,
            ContentChangeType remoteContent = ContentChangeType.NONE)
        {
            IMappedObject obj = this.Storage.GetObjectByRemoteId(remoteId.Id);

            if (remoteId is IFolder)
            {
                // Rename local folder
                IFolder        remoteFolder = remoteId as IFolder;
                IDirectoryInfo dirInfo      = localFile as IDirectoryInfo;
                string         oldPath      = dirInfo.FullName;
                try {
                    dirInfo.MoveTo(Path.Combine(dirInfo.Parent.FullName, remoteFolder.Name));
                    obj.Name = remoteFolder.Name;
                } catch (IOException) {
                    if (dirInfo.Name.Equals(remoteFolder.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        obj.Name = dirInfo.Name;
                    }
                    else
                    {
                        throw;
                    }
                }

                if (remoteFolder.LastModificationDate != null)
                {
                    dirInfo.LastWriteTimeUtc = (DateTime)remoteFolder.LastModificationDate;
                }


                obj.LastChangeToken        = remoteFolder.ChangeToken;
                obj.LastRemoteWriteTimeUtc = remoteFolder.LastModificationDate;
                obj.LastLocalWriteTimeUtc  = dirInfo.LastWriteTimeUtc;
                this.Storage.SaveMappedObject(obj);
                OperationsLogger.Info(string.Format("Renamed local folder {0} to {1}", oldPath, remoteFolder.Name));
            }
            else if (remoteId is IDocument)
            {
                // Rename local file
                IDocument remoteDocument = remoteId as IDocument;
                IFileInfo fileInfo       = localFile as IFileInfo;
                string    oldPath        = fileInfo.FullName;
                fileInfo.MoveTo(Path.Combine(fileInfo.Directory.FullName, remoteDocument.Name));
                if (remoteDocument.LastModificationDate != null)
                {
                    fileInfo.LastWriteTimeUtc = (DateTime)remoteDocument.LastModificationDate;
                }

                obj.Name                   = remoteDocument.Name;
                obj.LastChangeToken        = remoteContent == ContentChangeType.NONE ? remoteDocument.ChangeToken : obj.LastChangeToken;
                obj.LastRemoteWriteTimeUtc = remoteContent == ContentChangeType.NONE ? remoteDocument.LastModificationDate : obj.LastRemoteWriteTimeUtc;
                obj.LastLocalWriteTimeUtc  = fileInfo.LastWriteTimeUtc;
                this.Storage.SaveMappedObject(obj);
                OperationsLogger.Info(string.Format("Renamed local file {0} to {1}", oldPath, remoteDocument.Name));
                if (remoteContent != ContentChangeType.NONE)
                {
                    throw new ArgumentException("Remote documents content is also changed => force crawl sync.");
                }
            }
            else
            {
                throw new ArgumentException("Given remote Id is not an IFolder nor an IDocument instance");
            }
        }