Exemplo n.º 1
0
        /// <summary>
        /// Creates a new document with the specified name.
        /// </summary>
        /// <param name="name">The name of the new document.</param>
        /// <returns>
        /// The created <see cref="IWebDavStoreDocument" /> instance.
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavConflictException">If the item already exists</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access</exception>
        public IWebDavStoreDocument CreateDocument(string name)
        {
            string itemPath = Path.Combine(ItemPath, name);

            if (File.Exists(itemPath) || Directory.Exists(itemPath))
            {
                throw new WebDavConflictException();
            }

            try
            {
                // Impersonate the current user and delete the directory
                WindowsImpersonationContext wic = Identity.Impersonate();
                File.Create(itemPath).Dispose();
                wic.Undo();
            }
            catch
            {
                throw new WebDavUnauthorizedException();
            }

            WebDavDiskStoreDocument document = new WebDavDiskStoreDocument(this, itemPath);

            _items.Add(name, new WeakReference(document));
            return(document);
        }
        /// <summary>
        /// Retrieves a store item by its name.
        /// </summary>
        /// <param name="name">The name of the store item to retrieve.</param>
        /// <returns>
        /// The store item that has the specified
        /// <paramref name="name" />;
        /// or
        /// <c>null</c> if there is no store item with that name.
        /// </returns>
        public IWebDavStoreItem GetItemByName(string name)
        {
            var path = Path.Combine(ItemPath, name);


            IWebDavStoreItem document = null;

            WindowsIdentity.RunImpersonated(WindowsIdentity.GetCurrent().AccessToken, () =>
            {
                if (File.Exists(path) && CanReadFile(path))
                {
                    document = new WebDavDiskStoreDocument(this, path);
                }
                if (Directory.Exists(path) && CanReadDirectory(path))
                {
                    document = new WebDavDiskStoreCollection(this, path);
                }
            });
            return(document);

            //using (Identity.Impersonate())
            //{
            //    if (File.Exists(path) && CanReadFile(path))
            //        return new WebDavDiskStoreDocument(this, path);
            //    if (Directory.Exists(path) && CanReadDirectory(path))
            //        return new WebDavDiskStoreCollection(this, path);
            //}

            //return null;
        }
        /// <summary>
        /// Copies an existing store item into this collection, overwriting any existing items.
        /// </summary>
        /// <param name="source">The store item to copy from.</param>
        /// <param name="destinationName">The name of the copy to create of <paramref name="source" />.</param>
        /// <param name="includeContent">The boolean for copying the containing files/folders or not.</param>
        /// <returns>
        /// The created <see cref="IWebDavStoreItem" /> instance.
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access</exception>
        public IWebDavStoreItem CopyItemHere(IWebDavStoreItem source, string destinationName, bool includeContent)
        {
            // We get the path of
            var destinationItemPath = Path.Combine(ItemPath, destinationName);

            // the moving of the item
            if (source.IsCollection)
            {
                try
                {
                    // Impersonate the current user and move the directory

                    WindowsIdentity.RunImpersonated(WindowsIdentity.GetCurrent().AccessToken, () =>
                    {
                        DirectoryCopy(source.ItemPath, destinationItemPath, true);
                    });

                    //WindowsImpersonationContext wic = Identity.Impersonate();
                    //DirectoryCopy(source.ItemPath, destinationItemPath, true);
                    //wic.Undo();
                }
                catch
                {
                    throw new WebDavUnauthorizedException();
                }

                // We return the moved file as a WebDAVDiskStoreDocument
                var collection = new WebDavDiskStoreCollection(this, destinationItemPath);
                _items.Add(destinationName, new WeakReference(collection));
                return(collection);
            }


            // We copy the file with an override.
            try
            {
                // Impersonate the current user and copy the file
                WindowsIdentity.RunImpersonated(WindowsIdentity.GetCurrent().AccessToken, () =>
                {
                    File.Copy(source.ItemPath, destinationItemPath, true);
                });


                //    WindowsImpersonationContext wic = Identity.Impersonate();
                //    File.Copy(source.ItemPath, destinationItemPath, true);
                //    wic.Undo();
            }
            catch
            {
                throw new WebDavUnauthorizedException();
            }

            // We return the moved file as a WebDAVDiskStoreDocument
            var document = new WebDavDiskStoreDocument(this, destinationItemPath);

            _items.Add(destinationName, new WeakReference(document));
            return(document);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Moves an existing store item into this collection, overwriting any existing items.
        /// </summary>
        /// <param name="source">The store item to move.</param>
        /// <param name="destinationName">The
        /// <see cref="IWebDavStoreItem" /> that refers to the item that was moved,
        /// in its new location.</param>
        /// <returns>
        /// The moved <see cref="IWebDavStoreItem" /> instance.
        /// </returns>
        /// <exception cref="System.Exception">Path to the source item not defined.</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access</exception>
        /// <remarks>
        /// Note that the method should fail without creating or overwriting content in the
        /// target collection if the move cannot go through.
        /// </remarks>
        public IWebDavStoreItem MoveItemHere(IWebDavStoreItem source, string destinationName)
        {
            // try to get the path of the source item
            string sourceItemPath          = "";
            WebDavDiskStoreItem sourceItem = (WebDavDiskStoreItem)source;

            sourceItemPath = sourceItem.ItemPath;

            if (sourceItemPath.Equals(""))
            {
                throw new Exception("Path to the source item not defined.");
            }

            // We get the path of
            string destinationItemPath = Path.Combine(ItemPath, destinationName);

            // the moving of the item
            if (source.IsCollection)
            {
                try
                {
                    // Impersonate the current user and move the directory
                    WindowsImpersonationContext wic = Identity.Impersonate();
                    Directory.Move(sourceItemPath, destinationItemPath);
                    wic.Undo();
                }
                catch
                {
                    throw new WebDavUnauthorizedException();
                }

                // We return the moved file as a WebDAVDiskStoreDocument
                var collection = new WebDavDiskStoreCollection(this, destinationItemPath);
                _items.Add(destinationName, new WeakReference(collection));
                return(collection);
            }

            try
            {
                // Impersonate the current user and move the file
                WindowsImpersonationContext wic = Identity.Impersonate();
                File.Move(sourceItemPath, destinationItemPath);
                wic.Undo();
            }
            catch
            {
                throw new WebDavUnauthorizedException();
            }

            // We return the moved file as a WebDAVDiskStoreDocument
            WebDavDiskStoreDocument document = new WebDavDiskStoreDocument(this, destinationItemPath);

            _items.Add(destinationName, new WeakReference(document));
            return(document);
        }
        /// <summary>
        /// Moves an existing store item into this collection, overwriting any existing items.
        /// </summary>
        /// <param name="source">The store item to move.</param>
        /// <param name="destinationName">The
        /// <see cref="IWebDavStoreItem" /> that refers to the item that was moved,
        /// in its new location.</param>
        /// <returns>
        /// The moved <see cref="IWebDavStoreItem" /> instance.
        /// </returns>
        /// <exception cref="System.Exception">Path to the source item not defined.</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access</exception>
        /// <remarks>
        /// Note that the method should fail without creating or overwriting content in the
        /// target collection if the move cannot go through.
        /// </remarks>
        public IWebDavStoreItem MoveItemHere(IWebDavStoreItem source, string destinationName)
        {
            // try to get the path of the source item
            WebDavDiskStoreItem sourceItem = (WebDavDiskStoreItem)source;
            string sourceItemPath = sourceItem.ItemPath;

            if (sourceItemPath.Equals(string.Empty))
            {
                throw new Exception("Path to the source item not defined.");
            }

            // We get the path of
            string destinationItemPath = Path.Combine(ItemPath, destinationName);

            // the moving of the item
            if (source.IsCollection)
            {
                try
                {
                    // Impersonate the current user and move the directory
                    WindowsImpersonationContext wic = Identity.Impersonate();
                    Directory.Move(sourceItemPath, destinationItemPath);
                    wic.Undo();
                }
                catch
                {
                    throw new WebDavUnauthorizedException();
                }

                // We return the moved file as a WebDAVDiskStoreDocument
                var collection = new WebDavDiskStoreCollection(this, destinationItemPath);
                _items.Add(destinationName, new WeakReference(collection));
                return collection;
            }

            try
            {
                // Impersonate the current user and move the file
                WindowsImpersonationContext wic = Identity.Impersonate();
                File.Move(sourceItemPath, destinationItemPath);
                wic.Undo();
            }
            catch
            {
                throw new WebDavUnauthorizedException();
            }

            // We return the moved file as a WebDAVDiskStoreDocument
            WebDavDiskStoreDocument document = new WebDavDiskStoreDocument(this, destinationItemPath);
            _items.Add(destinationName, new WeakReference(document));
            return document;

        }
        /// <summary>
        /// Copies an existing store item into this collection, overwriting any existing items.
        /// </summary>
        /// <param name="source">The store item to copy from.</param>
        /// <param name="destinationName">The name of the copy to create of <paramref name="source" />.</param>
        /// <param name="includeContent">The boolean for copying the containing files/folders or not.</param>
        /// <returns>
        /// The created <see cref="IWebDavStoreItem" /> instance.
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access</exception>
        public IWebDavStoreItem CopyItemHere(IWebDavStoreItem source, string destinationName, bool includeContent)
        {
            // We get the path of
            string destinationItemPath = Path.Combine(ItemPath, destinationName);

            // the moving of the item
            if (source.IsCollection)
            {
                try
                {
                    // Impersonate the current user and move the directory
                    WindowsImpersonationContext wic = Identity.Impersonate();
                    DirectoryCopy(source.ItemPath, destinationItemPath, true);
                    wic.Undo();
                }
                catch
                {
                    throw new WebDavUnauthorizedException();
                }

                // We return the moved file as a WebDAVDiskStoreDocument
                WebDavDiskStoreCollection collection = new WebDavDiskStoreCollection(this, destinationItemPath);
                _items.Add(destinationName, new WeakReference(collection));
                return collection;
            }


            // We copy the file with an override.
            try
            {
                // Impersonate the current user and copy the file
                WindowsImpersonationContext wic = Identity.Impersonate();
                File.Copy(source.ItemPath, destinationItemPath, true);
                wic.Undo();
            }
            catch
            {
                throw new WebDavUnauthorizedException();
            }

            // We return the moved file as a WebDAVDiskStoreDocument
            WebDavDiskStoreDocument document = new WebDavDiskStoreDocument(this, destinationItemPath);
            _items.Add(destinationName, new WeakReference(document));
            return document;

        }
        /// <summary>
        /// Creates a new document with the specified name.
        /// </summary>
        /// <param name="name">The name of the new document.</param>
        /// <returns>
        /// The created <see cref="IWebDavStoreDocument" /> instance.
        /// </returns>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavConflictException">If the item already exists</exception>
        /// <exception cref="WebDAVSharp.Server.Exceptions.WebDavUnauthorizedException">If the user is unauthorized or has no access</exception>
        public IWebDavStoreDocument CreateDocument(string name)
        {
            string itemPath = Path.Combine(ItemPath, name);
            if (File.Exists(itemPath) || Directory.Exists(itemPath))
                throw new WebDavConflictException();

            try
            {
                // Impersonate the current user and delete the directory
                WindowsImpersonationContext wic = Identity.Impersonate();
                File.Create(itemPath).Dispose();
                wic.Undo();
            }
            catch
            {
                throw new WebDavUnauthorizedException();
            }

            WebDavDiskStoreDocument document = new WebDavDiskStoreDocument(this, itemPath);
            _items.Add(name, new WeakReference(document));
            return document;

        }