Esempio n. 1
0
        /// <summary>
        /// Gets CalDAV items.
        /// </summary>
        /// <param name="itemPath">Relative path requested.</param>
        /// <param name="context">Instance of <see cref="DavContext"/> class.</param>
        /// <returns>Object implementing various calendar items or null if no object corresponding to path is found.</returns>
        public static async Task <IHierarchyItemAsync> GetCalDavItemAsync(DavContext context, string itemPath)
        {
            // If this is [DAVLocation]/calendars - return folder that contains all calendars.
            if (itemPath.Equals(CalendarsRootFolder.CalendarsRootFolderPath.Trim('/'), System.StringComparison.InvariantCultureIgnoreCase))
            {
                return(new CalendarsRootFolder(context));
            }

            string[] segments = itemPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            // If URL ends with .ics - return calendar file, which contains event or to-do.
            if (itemPath.EndsWith(CalendarFile.Extension, System.StringComparison.InvariantCultureIgnoreCase))
            {
                string uid = EncodeUtil.DecodeUrlPart(Path.GetFileNameWithoutExtension(segments.Last()));
                return((await CalendarFile.LoadByUidsAsync(context, new[] { uid }, PropsToLoad.All)).FirstOrDefault());
            }

            // If this is [DAVLocation]/calendars/[CalendarFolderId]/ return calendar.
            if (itemPath.StartsWith(CalendarsRootFolder.CalendarsRootFolderPath.Trim('/'), System.StringComparison.InvariantCultureIgnoreCase))
            {
                Guid calendarFolderId;
                if (Guid.TryParse(EncodeUtil.DecodeUrlPart(segments.Last()), out calendarFolderId))

                {
                    return(await CalendarFolder.LoadByIdAsync(context, calendarFolderId));
                }
            }

            return(null);
        }
        /// <summary>
        /// Gets CardDAV items.
        /// </summary>
        /// <param name="path">Relative path requested.</param>
        /// <param name="context">Instance of <see cref="DavContext"/> class.</param>
        /// <returns>Object implementing various business card items or null if no object corresponding to path is found.</returns>
        public static async Task <IHierarchyItemAsync> GetCardDavItemAsync(DavContext context, string path)
        {
            // If this is [DAVLocation]/addressbooks - return folder that contains all addressbooks.
            if (path.Equals(AddressbooksRootFolder.AddressbooksRootFolderPath.Trim('/'), System.StringComparison.InvariantCultureIgnoreCase))
            {
                return(new AddressbooksRootFolder(context));
            }

            string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            // If URL ends with .vcf - return address book file, which contains vCard.
            if (path.EndsWith(CardFile.Extension, System.StringComparison.InvariantCultureIgnoreCase))
            {
                string fileName = EncodeUtil.DecodeUrlPart(System.IO.Path.GetFileNameWithoutExtension(segments.Last()));
                return((await CardFile.LoadByFileNamesAsync(context, new[] { fileName }, PropsToLoad.All)).FirstOrDefault());
            }

            // If this is [DAVLocation]/addressbooks/[AddressbookFolderId]/ return address book.
            if (path.StartsWith(AddressbooksRootFolder.AddressbooksRootFolderPath.Trim('/'), System.StringComparison.InvariantCultureIgnoreCase))
            {
                Guid addressbookFolderId;
                if (Guid.TryParse(EncodeUtil.DecodeUrlPart(segments.Last()), out addressbookFolderId))

                {
                    return(await AddressbookFolder.LoadByIdAsync(context, addressbookFolderId));
                }
            }

            return(null);
        }
 /// <summary>
 /// Returns the physical file path that corresponds to the specified virtual path on the Web server.
 /// </summary>
 /// <param name="relativePath">Path relative to WebDAV root folder.</param>
 /// <returns>Corresponding path in file system.</returns>
 internal string MapPath(string relativePath)
 {
     //Convert to local file system path by decoding every part, reversing slashes and appending
     //to repository root.
     string[] encodedParts = relativePath.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
     string[] decodedParts = encodedParts.Select <string, string>(p => EncodeUtil.DecodeUrlPart(p).Normalize(NormalizationForm.FormC)).ToArray();
     return(Path.Combine(RepositoryPath, string.Join(Path.DirectorySeparatorChar.ToString(), decodedParts)));
 }
Esempio n. 4
0
        /// <summary>
        /// Reads item from database by path.
        /// </summary>
        /// <param name="path">Item path.</param>
        /// <returns>Instance of <see cref="IHierarchyItemAsync"/>.</returns>
        private async Task <IHierarchyItemAsync> getItemByPathAsync(string path)
        {
            Guid id = rootId;

            string[] names = path.Split('/');
            int      last  = names.Length - 1;

            while (last > 0 && names[last] == string.Empty)
            {
                last--;
            }

            for (int i = 0; i < last; i++)
            {
                if (!string.IsNullOrEmpty(names[i]))
                {
                    object result = await ExecuteScalarAsync <object>(
                        @"SELECT 
                             ItemId
                          FROM Item
                          WHERE Name = @Name AND ParentItemId = @Parent",
                        "@Name", EncodeUtil.DecodeUrlPart(names[i]),
                        "@Parent", id);

                    if (result != null)
                    {
                        id = (Guid)result;
                    }
                    else
                    {
                        return(null);
                    }
                }
            }

            // get item properties
            string command =
                @"SELECT
                       ItemId
                     , ParentItemId
                     , ItemType
                     , Name
                     , Created
                     , Modified
                           
                     FROM Item
                  WHERE Name = @Name AND ParentItemId = @Parent";
            IList <DavHierarchyItem> davHierarchyItems = await ExecuteItemAsync <DavHierarchyItem>(
                string.Join("/", names, 0, last) + "/",
                command,
                "@Name", EncodeUtil.DecodeUrlPart(names[last]),
                "@Parent", id);

            return(davHierarchyItems.FirstOrDefault());
        }
Esempio n. 5
0
        /// <summary>
        /// Creates instance of <see cref="LogicalFolder"/> class.
        /// </summary>
        /// <param name="context">Instance of <see cref="DavContext"/></param>
        /// <param name="path">Encoded path relative to WebDAV root.</param>
        /// <param name="children">List of child items that will be returned when enumerating this folder children.</param>
        public LogicalFolder(DavContext context, string path, IEnumerable <IHierarchyItemAsync> children = null)
            : base(context)
        {
            this.Context  = context;
            this.itemPath = path;
            this.children = children ?? new IHierarchyItemAsync[0];

            path = path.TrimEnd('/');
            string encodedName = path.Substring(path.LastIndexOf('/') + 1);

            this.displayName = EncodeUtil.DecodeUrlPart(encodedName);
        }
Esempio n. 6
0
        /// <summary>
        /// Reads item from database by path.
        /// </summary>
        /// <param name="path">Item path.</param>
        /// <returns>Instance of <see cref="IHierarchyItemAsync"/>.</returns>
        private IHierarchyItem getItemByPath(string path)
        {
            Guid id = rootId;

            string[] names = path.Split('/');
            int      last  = names.Length - 1;

            while (last > 0 && names[last] == string.Empty)
            {
                last--;
            }

            for (int i = 0; i < last; i++)
            {
                if (!string.IsNullOrEmpty(names[i]))
                {
                    object result = ExecuteScalar <object>(
                        @"SELECT ID FROM DMS_Documents
                          WHERE Name = @Name AND ParentID = @Parent
                          UNION ALL
                          SELECT ID FROM DMS_Folders
                          WHERE Name = @Name AND ParentID = @Parent",
                        "@Name", EncodeUtil.DecodeUrlPart(names[i]),
                        "@Parent", id);

                    if (result != null)
                    {
                        id = (Guid)result;
                    }
                    else
                    {
                        return(null);
                    }
                }
            }

            // get item properties
            string command =
                @"SELECT ID, ParentID, 2 as ItemType, Name, CreatedOn, ModifiedOn, Attributes       
                     FROM DMS_Documents
                    WHERE Name = @Name AND ParentID = @Parent
                    UNION ALL
                    SELECT ID, ParentID, 3 as ItemType, Name, CreatedOn, ModifiedOn, Attributes       
                     FROM DMS_Folders
                    WHERE Name = @Name AND ParentID = @Parent";
            var davHierarchyItems = ExecuteItem <DavHierarchyItem>(
                string.Join("/", names, 0, last) + "/",
                command,
                "@Name", EncodeUtil.DecodeUrlPart(names[last]),
                "@Parent", id);

            return(davHierarchyItems.FirstOrDefault());
        }
Esempio n. 7
0
        /// <summary>
        /// Returns item info for the path. Doesn't check if item exists.
        /// </summary>
        /// <param name="path">Path to item.</param>
        /// <returns><see cref="DataCloudItem"/></returns>
        public async Task <DataCloudItem> GetItemAsync(string path)
        {
            var client     = GetFileClient(path);
            var properties = await client.GetPropertiesAsync();

            var dlItem = new DataCloudItem
            {
                ContentLength = properties.Value.ContentLength,
                ContentType   = properties.Value.ContentType,
                Name          = EncodeUtil.DecodeUrlPart(client.Name),
                Path          = client.Path,
                CreatedUtc    = properties.Value.CreatedOn.UtcDateTime,
                ModifiedUtc   = properties.Value.LastModified.UtcDateTime,
                Properties    = properties.Value.Metadata,
                IsDirectory   = properties.Value.IsDirectory
            };

            return(dlItem);
        }
Esempio n. 8
0
        /// <summary>
        /// Gets object representing ACL folder/user/group.
        /// </summary>
        /// <param name="path">Relative path requested.</param>
        /// <param name="context">Instance of <see cref="DavContext"/> class.</param>
        /// <returns>Object implemening LogicalFolder/IPrincipalFolder</returns>
        internal static async Task <IHierarchyItemAsync> GetAclItemAsync(DavContext context, string path)
        {
            //If this is /acl - return fake folder which contains users and groups.
            if (path == AclFolder.PREFIX)
            {
                return(new AclFolder(context));
            }

            //if this is /acl/users - return fake folder which contains users.
            if (path == UserFolder.PREFIX)
            {
                return(new UserFolder(context));
            }

            //if this is /acl/groups - return fake folder which contains groups.
            if (path == GroupFolder.PREFIX)
            {
                return(new GroupFolder(context));
            }

            //if this is /acl/users/<user name> - return instance of User.
            if (path.StartsWith(UserFolder.PATH))
            {
                string name = EncodeUtil.DecodeUrlPart(path.Substring(UserFolder.PATH.Length));
                //we don't need an exception here - so check for validity.
                if (PrincipalBase.IsValidUserName(name))
                {
                    return(User.FromName(name, context));
                }
            }

            //if this is /acl/groups/<group name> - return instance of Group.
            if (path.StartsWith(GroupFolder.PATH))
            {
                string name = EncodeUtil.DecodeUrlPart(path.Substring(GroupFolder.PATH.Length));
                if (PrincipalBase.IsValidUserName(name))
                {
                    return(Group.FromName(name, context));
                }
            }
            return(null);
        }
Esempio n. 9
0
        /// <summary>
        /// Gets object representing ACL folder, user or group.
        /// </summary>
        /// <param name="path">Relative path requested.</param>
        /// <param name="context">Instance of <see cref="DavContext"/> class.</param>
        /// <returns>Object implemening ACL principal or folder</returns>
        internal static async Task <IHierarchyItemAsync> GetAclItemAsync(DavContext context, string path)
        {
            // If this is [DAVLocation]/acl - return folder which contains users and groups.
            if (path.Equals(AclFolder.AclFolderPath.Trim('/'), System.StringComparison.InvariantCultureIgnoreCase))
            {
                return(new AclFolder(context));
            }

            // If this is [DAVLocation]/acl/users - return folder which contains users.
            if (path.Equals(UsersFolder.UsersFolderPath.Trim('/'), System.StringComparison.InvariantCultureIgnoreCase))
            {
                return(new UsersFolder(context));
            }

            // If this is [DAVLocation]/acl/users/[UserID] - return instance of User.
            if (path.StartsWith(UsersFolder.UsersFolderPath.Trim('/'), System.StringComparison.InvariantCultureIgnoreCase))
            {
                string[] segments = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                string   userId   = EncodeUtil.DecodeUrlPart(segments.Last());
                return(await User.GetUserAsync(context, userId));
            }

            return(null);
        }
Esempio n. 10
0
        /// <summary>
        /// Returns list of child items in current folder.
        /// </summary>
        /// <param name="relativePath">Relative path.</param>
        /// <returns>Returns list of child items in current folder.</returns>
        public async Task <IList <DataCloudItem> > GetChildrenAsync(string relativePath)
        {
            IList <DataCloudItem> children = new List <DataCloudItem>();

            await foreach (var pathItem in dataLakeClient.GetPathsAsync(EncodeUtil.DecodeUrlPart(relativePath)))
            {
                var path     = pathItem.Name;
                var realName = path;
                if (path.Contains("/"))
                {
                    realName = path.Substring(path.LastIndexOf("/", StringComparison.Ordinal) + 1);
                }
                children.Add(new DataCloudItem
                {
                    ContentLength = pathItem.ContentLength ?? 0,
                    Name          = realName,
                    Path          = EncodePath(pathItem.Name),
                    CreatedUtc    = pathItem.LastModified.UtcDateTime,
                    ModifiedUtc   = pathItem.LastModified.UtcDateTime,
                    IsDirectory   = pathItem.IsDirectory ?? false
                });
            }
            return(children);
        }
Esempio n. 11
0
 /// <summary>
 /// Helper method to create file client by path.
 /// </summary>
 /// <param name="relativePath">Relative path of item.</param>
 /// <returns>DataLakeFileClient or null if item is not exists.</returns>
 private DataLakeFileClient GetFileClient(string relativePath)
 {
     return(dataLakeClient.GetFileClient(EncodeUtil.DecodeUrlPart(relativePath == "" ? "\\/" : relativePath)));
 }