Example #1
0
        public void AppendContentStreamTest(
            string canonical_name,
            string localPath,
            string remoteFolderPath,
            string url,
            string user,
            string password,
            string repositoryId)
        {
            // var watch = Stopwatch.StartNew();
            ISession session = DotCMISSessionTests.CreateSession(user, password, url, repositoryId);

            // watch.Stop();
            // Console.WriteLine(String.Format("Created Session in {0} msec",watch.ElapsedMilliseconds));
            // watch.Restart();
            IFolder folder = (IFolder)session.GetObjectByPath(remoteFolderPath);

            // watch.Stop();
            // Console.WriteLine(String.Format("Requested folder in {0} msec", watch.ElapsedMilliseconds));
            string filename = "testfile.txt";
            Dictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add(PropertyIds.Name, filename);
            properties.Add(PropertyIds.ObjectTypeId, "cmis:document");
            try {
                IDocument doc = session.GetObjectByPath(remoteFolderPath + "/" + filename) as IDocument;
                if (doc != null)
                {
                    doc.Delete(true);
                }
            } catch (Exception) {
            }

            // watch.Restart();
            IDocument emptyDoc = folder.CreateDocument(properties, null, null);

            // watch.Stop();
            // Console.WriteLine(String.Format("Created empty doc in {0} msec", watch.ElapsedMilliseconds));
            Assert.That(emptyDoc.ContentStreamLength == 0 || emptyDoc.ContentStreamLength == null, "returned document shouldn't got any content");
            string content = "test";

            for (int i = 0; i < 10; i++)
            {
                ContentStream contentStream = new ContentStream();
                contentStream.FileName = filename;
                contentStream.MimeType = MimeType.GetMIMEType(filename);
                contentStream.Length   = content.Length;
                using (var memstream = new MemoryStream(Encoding.UTF8.GetBytes(content))) {
                    contentStream.Stream = memstream;
                    emptyDoc.AppendContentStream(contentStream, i == 9, true);
                }

                Assert.AreEqual(content.Length * (i + 1), emptyDoc.ContentStreamLength);
            }

            emptyDoc.DeleteAllVersions();
        }
Example #2
0
        public static IDocument AppendContent(this IDocument doc, string content, bool lastChunk = true)
        {
            ContentStream contentStream = new ContentStream();

            contentStream.FileName = doc.Name;
            contentStream.MimeType = MimeType.GetMIMEType(doc.Name);
            byte[] c = Encoding.UTF8.GetBytes(content);
            contentStream.Length = c.LongLength;
            using (var stream = new MemoryStream(c)) {
                contentStream.Stream = stream;
                return(doc.AppendContentStream(contentStream, lastChunk));
            }
        }
 /// <summary>
 ///  Appends the localFileStream to the remoteDocument.
 /// </summary>
 /// <returns>
 ///  The new CMIS document.
 /// </returns>
 /// <param name='remoteDocument'>
 ///  Remote document where the local content should be appended to.
 /// </param>
 /// <param name='localFileStream'>
 ///  Local file stream.
 /// </param>
 /// <param name='status'>
 ///  Transmission status where the uploader should report its appending status.
 /// </param>
 /// <param name='hashAlg'>
 ///  Hash alg which should be used to calculate a checksum over the appended content.
 /// </param>
 /// <exception cref="CmisSync.Lib.Tasks.UploadFailedException">If Upload fails</exception>
 public virtual IDocument AppendFile(IDocument remoteDocument, Stream localFileStream, Transmission transmission, HashAlgorithm hashAlg)
 {
     using (var transmissionStream = transmission.CreateStream(localFileStream))
         using (var hashstream = new CryptoStream(transmissionStream, hashAlg, CryptoStreamMode.Read)) {
             ContentStream contentStream = new ContentStream();
             contentStream.FileName = remoteDocument.Name;
             contentStream.MimeType = Cmis.MimeType.GetMIMEType(contentStream.FileName);
             contentStream.Stream   = hashstream;
             try {
                 return(remoteDocument.AppendContentStream(contentStream, true));
             } catch (Exception e) {
                 throw new UploadFailedException(e, remoteDocument);
             }
         }
 }
Example #4
0
 /// <summary>
 ///  Appends the localFileStream to the remoteDocument.
 /// </summary>
 /// <returns>
 ///  The new CMIS document.
 /// </returns>
 /// <param name='remoteDocument'>
 ///  Remote document where the local content should be appended to.
 /// </param>
 /// <param name='localFileStream'>
 ///  Local file stream.
 /// </param>
 /// <param name='status'>
 ///  Transmission status where the uploader should report its appending status.
 /// </param>
 /// <param name='hashAlg'>
 ///  Hash alg which should be used to calculate a checksum over the appended content.
 /// </param>
 /// <exception cref="CmisSync.Lib.Tasks.UploadFailedException">If Upload fails</exception>
 public virtual IDocument AppendFile(IDocument remoteDocument, Stream localFileStream, FileTransmissionEvent status, HashAlgorithm hashAlg)
 {
     using (ProgressStream progressstream = new ProgressStream(localFileStream, status))
         using (CryptoStream hashstream = new CryptoStream(progressstream, hashAlg, CryptoStreamMode.Read))
         {
             ContentStream contentStream = new ContentStream();
             contentStream.FileName = remoteDocument.Name;
             contentStream.MimeType = Cmis.MimeType.GetMIMEType(contentStream.FileName);
             contentStream.Stream   = hashstream;
             try {
                 return(remoteDocument.AppendContentStream(contentStream, true));
             } catch (Exception e) {
                 throw new UploadFailedException(e, remoteDocument);
             }
         }
 }
Example #5
0
        /// <summary>
        ///  Uploads the file.
        ///  Resumes an upload if the given localFileStream.Position is larger than zero.
        /// </summary>
        /// <returns>
        ///  The new CMIS document.
        /// </returns>
        /// <param name='remoteDocument'>
        ///  Remote document where the local content should be uploaded to.
        /// </param>
        /// <param name='localFileStream'>
        ///  Local file stream.
        /// </param>
        /// <param name='status'>
        ///  Transmission status where the uploader should report its uploading status.
        /// </param>
        /// <param name='hashAlg'>
        ///  Hash alg which should be used to calculate a checksum over the uploaded content.
        /// </param>
        /// <param name='overwrite'>
        ///  If true, the local content will overwrite the existing content.
        /// </param>
        /// <exception cref="CmisSync.Lib.Tasks.UploadFailedException">
        /// Contains the last successful remote document state. This is needed for continue a failed upload.
        /// </exception>
        public override IDocument UploadFile(IDocument remoteDocument, Stream localFileStream, FileTransmissionEvent status, HashAlgorithm hashAlg, bool overwrite = true)
        {
            IDocument result = remoteDocument;

            for (long offset = localFileStream.Position; offset < localFileStream.Length; offset += this.ChunkSize)
            {
                bool isFirstChunk = offset == 0;
                bool isLastChunk  = (offset + this.ChunkSize) >= localFileStream.Length;
                using (NonClosingHashStream hashstream = new NonClosingHashStream(localFileStream, hashAlg, CryptoStreamMode.Read))
                    using (ChunkedStream chunkstream = new ChunkedStream(hashstream, this.ChunkSize))
                        using (OffsetStream offsetstream = new OffsetStream(chunkstream, offset))
                            using (ProgressStream progressstream = new ProgressStream(offsetstream, status))
                            {
                                status.Status.Length         = localFileStream.Length;
                                status.Status.ActualPosition = offset;
                                chunkstream.ChunkPosition    = offset;

                                ContentStream contentStream = new ContentStream();
                                contentStream.FileName = remoteDocument.Name;
                                contentStream.MimeType = Cmis.MimeType.GetMIMEType(remoteDocument.Name);
                                if (isLastChunk)
                                {
                                    contentStream.Length = localFileStream.Length - offset;
                                }
                                else
                                {
                                    contentStream.Length = this.ChunkSize;
                                }

                                contentStream.Stream = progressstream;
                                try {
                                    if (isFirstChunk && result.ContentStreamId != null && overwrite)
                                    {
                                        result.DeleteContentStream(true);
                                    }

                                    result.AppendContentStream(contentStream, isLastChunk, true);
                                } catch (Exception e) {
                                    throw new UploadFailedException(e, result);
                                }
                            }
            }

            hashAlg.TransformFinalBlock(new byte[0], 0, 0);
            return(result);
        }
Example #6
0
            private bool UploadStreamInTrunk(string filePath, Stream fileStream, IDocument remoteDocument)
            {
                if (repoinfo.ChunkSize <= 0)
                {
                    return false;
                }

                string fileName = remoteDocument.Name;
                for (long offset = fileStream.Position; offset < fileStream.Length; offset += repoinfo.ChunkSize)
                {
                    bool isLastTrunk = false;
                    if (offset + repoinfo.ChunkSize >= fileStream.Length)
                    {
                        isLastTrunk = true;
                    }
                    Logger.Debug(String.Format("Uploading next chunk (size={1}) of {0}: {2} of {3} finished({4}%)", fileName, repoinfo.ChunkSize, offset, fileStream.Length, 100 * offset / fileStream.Length));
                    using (ChunkedStream chunkstream = new ChunkedStream(fileStream, repoinfo.ChunkSize))
                    {
                        chunkstream.ChunkPosition = offset;

                        ContentStream contentStream = new ContentStream();
                        contentStream.FileName = fileName;
                        contentStream.MimeType = MimeType.GetMIMEType(fileName);
                        contentStream.Length = repoinfo.ChunkSize;
                        if (isLastTrunk)
                        {
                            contentStream.Length = fileStream.Length - offset;
                        }
                        contentStream.Stream = chunkstream;
                        lock (disposeLock)
                        {
                            if (disposed)
                            {
                                throw new ObjectDisposedException("Uploading");
                            }
                            try
                            {
                                remoteDocument.AppendContentStream(contentStream, isLastTrunk);
                                Logger.Debug("Response of the server: " + offset.ToString());
                                database.SetFileServerSideModificationDate(filePath, remoteDocument.LastModificationDate);
                            }
                            catch (Exception ex)
                            {
                                Logger.Fatal("Upload failed: " + ex);
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
Example #7
0
        public void CheckinTest(
            string canonical_name,
            string localPath,
            string remoteFolderPath,
            string url,
            string user,
            string password,
            string repositoryId,
            string binding)
        {
            string subFolderName = "subFolder";
            string fileName      = "testFile.bin";
            string subFolderPath = remoteFolderPath.TrimEnd('/') + "/" + subFolderName;
            string filePath      = subFolderPath + "/" + fileName;

            ISession session = DotCMISSessionTests.CreateSession(user, password, url, repositoryId, binding);

            if (!session.ArePrivateWorkingCopySupported())
            {
                Assert.Ignore("PWCs are not supported");
            }

            try {
                IFolder dir = session.GetObjectByPath(remoteFolderPath.TrimEnd('/') + "/" + subFolderName) as IFolder;
                if (dir != null)
                {
                    dir.DeleteTree(true, null, true);
                }
            } catch (CmisObjectNotFoundException) {
            }

            IFolder folder    = (IFolder)session.GetObjectByPath(remoteFolderPath);
            IFolder subFolder = folder.CreateFolder(subFolderName);

            string content = "testContent";

            IDocument doc         = subFolder.CreateDocument(fileName, "testContent", checkedOut: true);
            IObjectId checkoutId  = doc.CheckOut();
            IDocument docCheckout = session.GetObject(checkoutId) as IDocument;

            Assert.AreEqual(doc.ContentStreamLength, docCheckout.ContentStreamLength);

            ContentStream contentStream = new ContentStream();

            contentStream.FileName = fileName;
            contentStream.MimeType = MimeType.GetMIMEType(fileName);
            contentStream.Length   = content.Length;
            for (int i = 0; i < 10; ++i)
            {
                using (var memstream = new MemoryStream(Encoding.UTF8.GetBytes(content))) {
                    contentStream.Stream = memstream;
                    docCheckout.AppendContentStream(contentStream, i == 9);
                }
                Assert.That(docCheckout.ContentStreamLength, Is.EqualTo(content.Length * (i + 2)));
            }

            IObjectId checkinId  = docCheckout.CheckIn(true, null, null, "checkin");
            IDocument docCheckin = session.GetObject(checkinId) as IDocument;

            docCheckin.Refresh();   //  refresh is required, or DotCMIS will re-use the cached properties if checinId is the same as doc.Id
            Assert.That(docCheckin.ContentStreamLength, Is.EqualTo(content.Length * (9 + 2)));
        }
Example #8
0
        public void AppendContentStreamTest(
            string canonical_name,
            string localPath,
            string remoteFolderPath,
            string url,
            string user,
            string password,
            string repositoryId)
        {
            ISession session = DotCMISSessionTests.CreateSession(user, password, url, repositoryId);

            IFolder folder = (IFolder)session.GetObjectByPath(remoteFolderPath);

            string filename = "testfile.txt";
            Dictionary <string, object> properties = new Dictionary <string, object>();

            properties.Add(PropertyIds.Name, filename);
            properties.Add(PropertyIds.ObjectTypeId, "cmis:document");
            IDocument doc = null;

            try {
                doc = session.GetObjectByPath(remoteFolderPath.TrimEnd('/') + "/" + filename) as IDocument;
                if (doc != null)
                {
                    doc.Delete(true);
                }
            } catch (Exception) {
            }
            string content = "test";

            doc = folder.CreateDocument(filename, content);
            Assert.That(doc.ContentStreamLength == content.Length, "returned document should have got content");
            for (int i = 0; i < 10; i++)
            {
                ContentStream contentStream = new ContentStream();
                contentStream.FileName = filename;
                contentStream.MimeType = MimeType.GetMIMEType(filename);
                contentStream.Length   = content.Length;
                using (var memstream = new MemoryStream(Encoding.UTF8.GetBytes(content))) {
                    contentStream.Stream = memstream;
                    doc.AppendContentStream(contentStream, i == 9, true);
                }

                Assert.AreEqual(content.Length * (i + 2), doc.ContentStreamLength);
            }

            for (int i = 0; i < 10; i++)
            {
                ContentStream contentStream = new ContentStream();
                contentStream.FileName = filename;
                contentStream.MimeType = MimeType.GetMIMEType(filename);
                contentStream.Length   = content.Length;
                using (var memstream = new MemoryStream(Encoding.UTF8.GetBytes(content))) {
                    contentStream.Stream = memstream;
                    doc.AppendContentStream(contentStream, true, true);
                }

                Assert.AreEqual(content.Length * (i + 2 + 10), doc.ContentStreamLength);
            }

            doc.DeleteAllVersions();
        }
Example #9
0
        /// <summary>
        ///  Uploads the file.
        ///  Resumes an upload if the given localFileStream.Position is larger than zero.
        /// </summary>
        /// <returns>
        ///  The new CMIS document.
        /// </returns>
        /// <param name='remoteDocument'>
        ///  Remote document where the local content should be uploaded to.
        /// </param>
        /// <param name='localFileStream'>
        ///  Local file stream.
        /// </param>
        /// <param name='transmission'>
        ///  Transmission status where the uploader should report its uploading status.
        /// </param>
        /// <param name='hashAlg'>
        ///  Hash alg which should be used to calculate a checksum over the uploaded content.
        /// </param>
        /// <param name='overwrite'>
        ///  If true, the local content will overwrite the existing content.
        /// </param>
        /// <param name="update">Is called on every new chunk, if not <c>null</c>.</param>
        /// <exception cref="CmisSync.Lib.Tasks.UploadFailedException">
        /// Contains the last successful remote document state. This is needed for continue a failed upload.
        /// </exception>
        public override IDocument UploadFile(
            IDocument remoteDocument,
            Stream localFileStream,
            Transmission transmission,
            HashAlgorithm hashAlg,
            bool overwrite        = true,
            UpdateChecksum update = null)
        {
            IDocument result = remoteDocument;

            for (long offset = localFileStream.Position; offset < localFileStream.Length; offset += this.ChunkSize)
            {
                bool isFirstChunk = offset == 0;
                bool isLastChunk  = (offset + this.ChunkSize) >= localFileStream.Length;
                using (var hashstream = new NonClosingHashStream(localFileStream, hashAlg, CryptoStreamMode.Read))
                    using (var chunkstream = new ChunkedStream(hashstream, this.ChunkSize))
                        using (var offsetstream = new OffsetStream(chunkstream, offset))
                            using (var transmissionStream = transmission.CreateStream(offsetstream)) {
                                transmission.Length       = localFileStream.Length;
                                transmission.Position     = offset;
                                chunkstream.ChunkPosition = offset;

                                ContentStream contentStream = new ContentStream();
                                contentStream.FileName = remoteDocument.Name;
                                contentStream.MimeType = Cmis.MimeType.GetMIMEType(remoteDocument.Name);
                                if (isLastChunk)
                                {
                                    contentStream.Length = localFileStream.Length - offset;
                                }
                                else
                                {
                                    contentStream.Length = this.ChunkSize;
                                }

                                contentStream.Stream = transmissionStream;
                                try {
                                    if (isFirstChunk && result.ContentStreamId != null && overwrite)
                                    {
                                        result.DeleteContentStream(true);
                                    }

                                    result.AppendContentStream(contentStream, isLastChunk, true);
                                    HashAlgorithmReuse reuse = hashAlg as HashAlgorithmReuse;
                                    if (reuse != null && update != null)
                                    {
                                        using (HashAlgorithm hash = (HashAlgorithm)reuse.Clone()) {
                                            hash.TransformFinalBlock(new byte[0], 0, 0);
                                            update(hash.Hash, result.ContentStreamLength.GetValueOrDefault());
                                        }
                                    }
                                } catch (Exception e) {
                                    if (e is FileTransmission.AbortException)
                                    {
                                        throw;
                                    }

                                    if (e.InnerException is FileTransmission.AbortException)
                                    {
                                        throw e.InnerException;
                                    }

                                    throw new UploadFailedException(e, result);
                                }
                            }
            }

            hashAlg.TransformFinalBlock(new byte[0], 0, 0);
            return(result);
        }
 /// <summary>
 ///  Appends the localFileStream to the remoteDocument.
 /// </summary>
 /// <returns>
 ///  The new CMIS document.
 /// </returns>
 /// <param name='remoteDocument'>
 ///  Remote document where the local content should be appended to.
 /// </param>
 /// <param name='localFileStream'>
 ///  Local file stream.
 /// </param>
 /// <param name='status'>
 ///  Transmission status where the uploader should report its appending status.
 /// </param>
 /// <param name='hashAlg'>
 ///  Hash alg which should be used to calculate a checksum over the appended content.
 /// </param>
 /// <exception cref="CmisSync.Lib.Tasks.UploadFailedException">If Upload fails</exception>
 public virtual IDocument AppendFile(IDocument remoteDocument, Stream localFileStream, Transmission transmission, HashAlgorithm hashAlg) {
     using (var transmissionStream = transmission.CreateStream(localFileStream))
     using (var hashstream = new CryptoStream(transmissionStream, hashAlg, CryptoStreamMode.Read)) {
         ContentStream contentStream = new ContentStream();
         contentStream.FileName = remoteDocument.Name;
         contentStream.MimeType = Cmis.MimeType.GetMIMEType(contentStream.FileName);
         contentStream.Stream = hashstream;
         try {
             return remoteDocument.AppendContentStream(contentStream, true);
         } catch(Exception e) {
             throw new UploadFailedException(e, remoteDocument);
         }
     }
 }