CmisSync.Lib.Consumer.SituationSolver.PWC.LocalObjectAddedWithPWC.Solve C# (CSharp) Метод

Solve() публичный Метод

Solve the specified situation by using localFile and remote object.
public Solve ( IFileSystemInfo localFileSystemInfo, IObjectId remoteId, ContentChangeType localContent = ContentChangeType.NONE, ContentChangeType remoteContent = ContentChangeType.NONE ) : void
localFileSystemInfo IFileSystemInfo Local filesystem info instance.
remoteId IObjectId Remote identifier or object.
localContent ContentChangeType Hint if the local content has been changed.
remoteContent ContentChangeType Information if the remote content has been changed.
Результат void
        public override void Solve(
            IFileSystemInfo localFileSystemInfo,
            IObjectId remoteId,
            ContentChangeType localContent = ContentChangeType.NONE,
            ContentChangeType remoteContent = ContentChangeType.NONE)
        {
            if (localFileSystemInfo is IFileInfo) {
                IFileInfo localFile = localFileSystemInfo as IFileInfo;
                localFile.Refresh();
                if (!localFile.Exists) {
                    throw new FileNotFoundException(string.Format("Local file {0} has been renamed/moved/deleted", localFile.FullName));
                }

                if (localFile.Length == 0) {
                    this.folderOrEmptyFileAddedSolver.Solve(localFileSystemInfo, null, localContent, remoteContent);
                    return;
                }

                string parentId = this.Storage.GetRemoteId(localFile.Directory);
                if (parentId == null) {
                    if (this.IsParentReadOnly(localFile)) {
                        return;
                    } else {
                        throw new ArgumentException("ParentId is null => invoke crawl sync to create parent first");
                    }
                }

                IDocument remoteDocument;
                try {
                    remoteDocument = this.CreateOrLoadExistingRemoteDocument(localFile, new ObjectId(parentId));
                } catch (CmisPermissionDeniedException e) {
                    OperationsLogger.Warn(string.Format("Permission denied while trying to Create the locally added object {0} on the server ({1}).", localFile.FullName, e.Message));
                    return;
                }

                Guid uuid = this.WriteOrUseUuidIfSupported(localFile);

                var transmission = this.transmissionManager.CreateTransmission(TransmissionType.UPLOAD_NEW_FILE, localFile.FullName);

                MappedObject mapped = new MappedObject(
                    localFile.Name,
                    remoteDocument.Id,
                    MappedObjectType.File,
                    parentId,
                    remoteDocument.ChangeToken) {
                        Guid = uuid,
                        LastRemoteWriteTimeUtc = remoteDocument.LastModificationDate,
                        LastLocalWriteTimeUtc = (DateTime?)localFileSystemInfo.LastWriteTimeUtc,
                        LastChangeToken = remoteDocument.ChangeToken,
                        LastContentSize = 0,
                        ChecksumAlgorithmName = "SHA-1",
                        LastChecksum = SHA1.Create().ComputeHash(new byte[0])
                    };

                Stopwatch watch = new Stopwatch();
                OperationsLogger.Debug(string.Format("Uploading file content of {0}", localFile.FullName));
                watch.Start();
                try {
                    mapped.LastChecksum = this.UploadFileWithPWC(localFile, ref remoteDocument, transmission);
                    mapped.ChecksumAlgorithmName = "SHA-1";
                    mapped.RemoteObjectId = remoteDocument.Id;
                } catch (Exception ex) {
                    if (ex is UploadFailedException && (ex as UploadFailedException).InnerException is CmisStorageException) {
                        OperationsLogger.Warn(string.Format("Could not upload file content of {0}:", localFile.FullName), (ex as UploadFailedException).InnerException);
                        return;
                    }

                    throw;
                }
                watch.Stop();

                mapped.LastContentSize = localFile.Length;
                mapped.LastChangeToken = remoteDocument.ChangeToken;
                mapped.LastRemoteWriteTimeUtc = remoteDocument.LastModificationDate;
                mapped.LastLocalWriteTimeUtc = localFileSystemInfo.LastWriteTimeUtc;

                this.Storage.SaveMappedObject(mapped);
                OperationsLogger.Info(string.Format("Uploaded file content of {0} in [{1} msec]", localFile.FullName, watch.ElapsedMilliseconds));
            } else {
                this.folderOrEmptyFileAddedSolver.Solve(localFileSystemInfo, remoteId, localContent, remoteContent);
            }
        }

Usage Example

        public void NewDirectoriesCallsArePassedToTheGivenSolver() {
            this.SetUpMocks();
            var folderSolver = new Mock<ISolver>();
            var undertest = new LocalObjectAddedWithPWC(
                this.session.Object,
                this.storage.Object,
                this.transmissionStorage.Object,
                this.manager.Object,
                folderSolver.Object);
            var localFolder = new Mock<IDirectoryInfo>();

            undertest.Solve(localFolder.Object, null, ContentChangeType.CREATED, ContentChangeType.NONE);

            folderSolver.Verify(s => s.Solve(localFolder.Object, null, ContentChangeType.CREATED, ContentChangeType.NONE), Times.Once());
        }