Skip to main content
Tab

FileSystemProviderBase.Exists(FileManagerFolder) Method

Override this method to implement the folder existence verification.

Namespace: DevExpress.Web

Assembly: DevExpress.Web.v24.1.dll

NuGet Package: DevExpress.Web

Declaration

public virtual bool Exists(
    FileManagerFolder folder
)

Parameters

Name Type Description
folder FileManagerFolder

A FileManagerFolder object that is the processed folder.

Returns

Type Description
Boolean

true if the folder exists within a file manager; otherwise, false.

Remarks

If you create a custom file system provider inherited from the FileSystemProviderBase class, you should override the Exists method to implement the folder existence verification.

Example

using DevExpress.Web.ASPxFileManager;
using System.IO;
public class LinqFileSystemProvider : FileSystemProviderBase {
    const int DbRootItemId = 1;
    DbFileSystemDataContext dataContext;
    Dictionary<int, DbFileSystemItem> folderCache;
    string rootFolderDisplayName;

    public LinqFileSystemProvider(string rootFolder)
        : base(rootFolder) {
        this.dataContext = new DbFileSystemDataContext();
        RefreshFolderCache();
    }

    public DbFileSystemDataContext DataContext { get { return dataContext; } }

    // Used to decrease the number of recursive LINQ to SQL queries made to a database
    public Dictionary<int, DbFileSystemItem> FolderCache { get { return folderCache; } }


    public override string RootFolderDisplayName { get { return rootFolderDisplayName; } }

    public override IEnumerable<FileManagerFile> GetFiles(FileManagerFolder folder) {
        DbFileSystemItem dbFolderItem = FindDbFolderItem(folder);
        return
            from dbItem in DataContext.DbFileSystemItems
            where !dbItem.IsFolder && dbItem.ParentId == dbFolderItem.Id
            select new FileManagerFile(this, folder, dbItem.Name);
    }
    public override IEnumerable<FileManagerFolder> GetFolders(FileManagerFolder parentFolder) {
        DbFileSystemItem dbFolderItem = FindDbFolderItem(parentFolder);
        return
            from dbItem in FolderCache.Values
            where dbItem.IsFolder && dbItem.ParentId == dbFolderItem.Id
            select new FileManagerFolder(this, parentFolder, dbItem.Name);
    }
    public override bool Exists(FileManagerFile file) {
        return FindDbFileItem(file) != null;
    }
    public override bool Exists(FileManagerFolder folder) {
        return FindDbFolderItem(folder) != null;
    }
    public override System.IO.Stream ReadFile(FileManagerFile file) {
        return new MemoryStream(FindDbFileItem(file).Data.ToArray());
    }
    public override DateTime GetLastWriteTime(FileManagerFile file) {
        var dbFileItem = FindDbFileItem(file);
        return dbFileItem.LastWriteTime.GetValueOrDefault(DateTime.Now);
    }

    // File/folder management operations
    public override void CreateFolder(FileManagerFolder parent, string name) {
        UpdateAndSubmitChanges(parent,
            dbItem => DataContext.DbFileSystemItems.InsertOnSubmit(new DbFileSystemItem() {
                IsFolder = true,
                LastWriteTime = DateTime.Now,
                Name = name,
                ParentId = dbItem.Id
            }));
    }
    public override void DeleteFile(FileManagerFile file) {
        UpdateAndSubmitChanges(file, dbItem =>
DataContext.DbFileSystemItems.DeleteOnSubmit(dbItem));
    }
    public override void DeleteFolder(FileManagerFolder folder) {
        UpdateAndSubmitChanges(folder, dbItem =>
DataContext.DbFileSystemItems.DeleteOnSubmit(dbItem));
    }
    public override void MoveFile(FileManagerFile file, FileManagerFolder newParentFolder) {
        UpdateAndSubmitChanges(file, dbItem => dbItem.ParentId =
FindDbFolderItem(newParentFolder).Id);
    }
    public override void MoveFolder(FileManagerFolder folder, FileManagerFolder newParentFolder)
{
        UpdateAndSubmitChanges(folder, dbItem => dbItem.ParentId =
FindDbFolderItem(newParentFolder).Id);
    }
    public override void RenameFile(FileManagerFile file, string name) {
        UpdateAndSubmitChanges(file, dbItem => dbItem.Name = name);
    }
    public override void RenameFolder(FileManagerFolder folder, string name) {
        UpdateAndSubmitChanges(folder, dbItem => dbItem.Name = name);
    }
    public override void UploadFile(FileManagerFolder folder, string fileName, Stream content) {
        UpdateAndSubmitChanges(folder,
            dbItem => DataContext.DbFileSystemItems.InsertOnSubmit(new DbFileSystemItem() {
                Name = fileName,
                Data = ReadAllBytes(content),
                ParentId = dbItem.Id,
                LastWriteTime = DateTime.Now,
                IsFolder = false
            }));
    }

    protected void UpdateAndSubmitChanges(FileManagerFile file, Action<DbFileSystemItem> update)
{
        UpdateAndSubmitChangesCore(FindDbFileItem(file), update);
    }
    protected void UpdateAndSubmitChanges(FileManagerFolder folder, Action<DbFileSystemItem>
update) {
        UpdateAndSubmitChangesCore(FindDbFolderItem(folder), update);
    }
    protected void UpdateAndSubmitChangesCore(DbFileSystemItem dbitem, Action<DbFileSystemItem>
update) {
        update(dbitem);
        DataContext.SubmitChanges();
        RefreshFolderCache();
    }
    protected DbFileSystemItem FindDbFileItem(FileManagerFile file) {
        DbFileSystemItem dbFolderItem = FindDbFolderItem(file.Folder);
        if(dbFolderItem == null)
            return null;
        return
            (from dbItem in DataContext.DbFileSystemItems
             where dbItem.ParentId == dbFolderItem.Id && !dbItem.IsFolder && dbItem.Name ==
file.Name
             select dbItem).FirstOrDefault();
    }
    protected DbFileSystemItem FindDbFolderItem(FileManagerFolder folder) {
        return
            (from dbItem in FolderCache.Values
             where dbItem.IsFolder && GetRelativeName(dbItem) == folder.RelativeName
             select dbItem).FirstOrDefault();
    }

    // Returns the path to a specified folder relative to a root folder
    protected string GetRelativeName(DbFileSystemItem dbFolder) {
        if(dbFolder.Id == DbRootItemId) return string.Empty;
        if(dbFolder.ParentId == DbRootItemId) return dbFolder.Name;
        if(!FolderCache.ContainsKey(dbFolder.ParentId)) return null;
        string name = GetRelativeName(FolderCache[dbFolder.ParentId]);
        return name == null ? null : Path.Combine(name, dbFolder.Name);
    }

    // Caches folder names in memory and obtains a root folder's name
    protected void RefreshFolderCache() {
        this.folderCache = (
            from dbItem in DataContext.DbFileSystemItems
            where dbItem.IsFolder
            select dbItem
        ).ToDictionary(i => i.Id);

        this.rootFolderDisplayName = (
            from dbItem in FolderCache.Values
            where dbItem.Id == DbRootItemId
            select dbItem.Name).First();
    }
    protected static byte[] ReadAllBytes(Stream stream) {
        byte[] buffer = new byte[16 * 1024];
        int readCount;
        using(MemoryStream ms = new MemoryStream()) {
            while((readCount = stream.Read(buffer, 0, buffer.Length)) > 0) {
                ms.Write(buffer, 0, readCount);
            }
            return ms.ToArray();
        }
    }
}
See Also