This is a migrated thread and some comments may be shown as answers.

Custom FileBrowserContentProvider

1 Answer 148 Views
Editor
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 07 May 2009, 04:28 PM
I am currently working to implement a custom file browser that writes to a database instead of the file system. I have looked through the implementation that you have provided, but my requirements are a little difference (of course).

Unless I explicitly set the UploadPaths on the document manager for every path in the system, I am unable to upload any documents. I need to be able to specify the root path and allow users to work in that path without restriction once access has been granted. Here is the implementation of my provider:

Imports Microsoft.VisualBasic  
Imports Telerik.Web.UI.Widgets  
Imports SWBTS.HelpDesk.Data  
Imports SWBTS.HelpDesk.Data.Entities  
 
Namespace HelpDeskContentProvider  
    Public Class DatabaseAttachmentContentProvider  
        Inherits FileBrowserContentProvider  
        Dim _Repository As New Repository(System.Configuration.ConfigurationManager.ConnectionStrings("HelpDesk").ConnectionString)  
 
        Public Sub New(ByVal context As HttpContext, ByVal searchPatterns As String(), ByVal viewPaths As String(), ByVal uploadPaths As String(), ByVal deletePaths As String(), ByVal selectedUrl As StringByVal selectedItemTag As String)  
            MyBase.New(context, searchPatterns, viewPaths, uploadPaths, deletePaths, selectedUrl, selectedItemTag)  
        End Sub 
 
        Public Overrides Function CreateDirectory(ByVal path As StringByVal name As StringAs String 
 
        End Function 
 
        Public Overrides Function DeleteDirectory(ByVal path As StringAs String 
            Throw New NotImplementedException("The deletion of folders is not supported. This must be done by your system database administrator.")  
        End Function 
 
        Public Overrides Function DeleteFile(ByVal path As StringAs String 
 
        End Function 
 
        Public Overrides Function GetFile(ByVal url As StringAs System.IO.Stream  
 
        End Function 
 
        Public Overrides Function GetFileName(ByVal url As StringAs String 
 
        End Function 
 
        Public Overrides Function GetPath(ByVal url As StringAs String 
 
        End Function 
 
        Public Overrides Function ResolveDirectory(ByVal path As StringAs Telerik.Web.UI.Widgets.DirectoryItem  
            Dim directories As DirectoryItem()  
            Dim currentFolder As FileFolder = _Repository.FileFolders.GetFolder(path)  
            If DisplayMode = FileBrowserDisplayMode.Tree Then 
                directories = GetSubFolders(currentFolder.FolderID)  
            End If 
            Return New DirectoryItem(currentFolder.FolderName, String.Empty, currentFolder.FolderID, currentFolder.FolderName, PathPermissions.Read And PathPermissions.Upload, GetFilesInFolder(currentFolder.FolderID), directories)  
        End Function 
 
        Public Overrides Function ResolveRootDirectoryAsList(ByVal path As StringAs Telerik.Web.UI.Widgets.DirectoryItem()  
            Dim folderTree As List(Of FileFolder) = _Repository.FileFolders.GetFolders()  
 
            Dim rootFolder As FileFolder = _Repository.FileFolders.GetFolder(path)  
            Return GetSubFolders(rootFolder.FolderID)  
        End Function 
 
        Public Overrides Function ResolveRootDirectoryAsTree(ByVal path As StringAs Telerik.Web.UI.Widgets.DirectoryItem  
            Dim folderTree As List(Of FileFolder) = _Repository.FileFolders.GetFolders()  
 
            Dim rootFolder As FileFolder = (From folder In folderTree Where folder.FolderID = path).Single()  
            Dim item As DirectoryItem = CreateDirectoryItem(rootFolder)  
 
            Return item  
        End Function 
 
        Public Overrides Function StoreBitmap(ByVal bitmap As System.Drawing.Bitmap, ByVal url As StringByVal format As System.Drawing.Imaging.ImageFormat) As String 
 
        End Function 
 
        Public Overloads Overrides Function StoreFile(ByVal file As Telerik.Web.UI.UploadedFile, ByVal path As StringByVal name As StringByVal ParamArray arguments() As StringAs String 
            Dim fileName As String = file.GetName()  
            Dim length As Integer = file.ContentLength  
            Dim contentType As String = file.ContentType  
            Dim extension As String = file.GetExtension()  
            Dim data(file.InputStream.Length) As Byte 
            Dim storagePath As String = StripSlashes(path)  
            file.InputStream.Read(data, 0, file.InputStream.Length)  
            Dim user As SystemUser = _Repository.Users.GetUser(My.User.Name.ToLower())  
 
            Dim newFileId As Integer 
            newFileId = _Repository.Files.CreateNewFile(fileName, extension, storagePath, length, contentType, data, user).FileID  
            Return "FileHandler.ashx?fileID=" & newFileId.ToString()  
        End Function 
 
        Private Function GetFilesInFolder(ByVal folderID As IntegerAs FileItem()  
            Dim files As List(Of FileItem) = New List(Of FileItem)  
            Dim fileList As List(Of DatabaseFileListItem) = _Repository.Files.GetFilesForFolder(folderID)  
            For Each FileItem As DatabaseFileListItem In fileList  
                Dim file As New FileItem(FileItem.FileName, FileItem.Extension, FileItem.ContentLength, String.Empty, "FileHandler.ashx?fileID=" & FileItem.FileID, String.Empty, PathPermissions.Read And PathPermissions.Delete)  
                files.Add(file)  
            Next 
            Return files.ToArray()  
        End Function 
 
        Private Function GetSubFolders(ByVal folderID As IntegerAs DirectoryItem()  
            Dim folders As New List(Of DirectoryItem)  
            Dim subFolders = _Repository.FileFolders.GetSubFolders(folderID)  
            For Each folder In subFolders  
                Dim folderItem As DirectoryItem = CreateDirectoryItem(folder)  
                folders.Add(folderItem)  
            Next 
            Return folders.ToArray()  
        End Function 
 
        Private Function CreateDirectoryItem(ByVal folderItem As FileFolder) As DirectoryItem  
            Dim folderName As String = folderItem.FolderName  
            Dim folderLocation As String = folderItem.FolderID  
            Dim folderFullPath As String = folderItem.FolderID  
            Dim folderTag As String = folderItem.FolderID  
            Dim files() As FileItem = GetFilesInFolder(folderItem.FolderID)  
            Dim directories() As DirectoryItem = GetSubFolders(folderItem.FolderID)  
            Dim permission As PathPermissions = PathPermissions.Delete Or PathPermissions.Read Or PathPermissions.Upload  
 
            Dim Item As New DirectoryItem(folderName, folderLocation, folderFullPath, folderTag, permission, files, directories)  
            Return Item  
        End Function 
 
        Private Function StripSlashes(ByVal path As StringAs String 
            path = path.Replace("/""")  
            Return path  
        End Function 
    End Class 
End Namespace 
 

Notice, for every directory item that I create, I set the permissions:
            Dim permission As PathPermissions = PathPermissions.Delete Or PathPermissions.Read Or PathPermissions.Upload  
 

However, this is ignored. If I set the permissions via code or declaritively, it works just fine.

<DocumentManager ContentProviderTypeName="HelpDeskContentProvider.DatabaseAttachmentContentProvider, App_Code" ViewPaths="1" UploadPaths="1" DeletePaths="1"/>  
 


Thanks for any tips or advice in what I'm missing.

1 Answer, 1 is accepted

Sort by
0
Lini
Telerik team
answered on 14 May 2009, 07:24 AM
Hi Chris,

Because of some security restrictions in version Q1 2009 SP1 (2009.1.402) it is not possible to upload new files in the file explorer unless the upload path in the UploadPaths collection or is a child of one of the paths in the collection. To determine if a path is a child of another path, we simply check if one path starts with the other. This presented a problem with some custom content providers, where it is possible for a child path and a parent path to be very different. We fixed this problem and the next RadControls service pack release it will be possible to upload files without specifying each individual path in the UploadPaths collection. As a matter of fact - the new version will skip the UploadPaths check if you set UploadPaths="/" and set the folder permissions from your content provider code.

Kind regards,
Lini
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Editor
Asked by
Chris
Top achievements
Rank 1
Answers by
Lini
Telerik team
Share this question
or