Example #1
0
        public string GetWebUrl(IVirtualFileItem item)
        {
            item.ThrowIfNull("item");

            if (item.FileSystem != this)
            {
                return item.FileSystem.GetWebUrl(item);
            }

            if (!string.IsNullOrEmpty(this.RootWebUrl))
            {
                UrlBuilder builder = new UrlBuilder(this.RootWebUrl);

                var relativePath = item.RelativePath;
                var rootFolderPath = this.rootFolderName + "/";
                if (relativePath.StartsWith(rootFolderPath, StringComparison.OrdinalIgnoreCase) || relativePath.StartsWith("/" + rootFolderPath, StringComparison.OrdinalIgnoreCase))
                {
                    relativePath = relativePath.ReplaceFirst(rootFolderPath, string.Empty);
                }

                builder.AppendUrl(relativePath);

                return builder.ToString();
            }

            return string.Empty;
        }
Example #2
0
        public string GetAbsolutePath(IVirtualFileItem item)
        {
            item.ThrowIfNull("item");

            if (item.FileSystem != this)
            {
                return item.FileSystem.GetAbsolutePath(item);
            }

            return this.GetAbsolutePath(item.RelativePath);
        }
Example #3
0
        /// <summary>
        /// Audits a request for a file resource that was not found on the file system.
        /// </summary>
        public static void AuditRequestedFileNotFound(IAuditor auditor, IVirtualFileItem file, FileSystemTask task)
        {
            if (!AuditHelper.IsWarnEnabledFor(auditor, task))
            {
                return;
            }

            string msg = "Could not handle request for file [{0}] - the file was not found on the file system.\n\n{1}";

            msg = String.Format(msg, file.ResourceInfo.Name, AuditHelper.CreateResourceInfoString(file));
            auditor.Audit(AuditLevel.Warning, task, AuditEvent.FileNotFound, msg);
        }
Example #4
0
        /// <summary>
        /// Audits a request for the file system's root folder.
        /// </summary>
        public static void AuditFileParentRequest(IAuditor auditor, IVirtualFileItem childFile, IVirtualFolderItem parentFolder)
        {
            if (!AuditHelper.IsInfoEnabledFor(auditor, FileSystemTask.FileParentRequest))
            {
                return;
            }

            string msg = "Parent folder of file [{0}] was requested.\nSubmitted file:\n{1}\n\nParent folder:\n{2}";

            msg = String.Format(msg, childFile.ResourceInfo.Name, AuditHelper.CreateResourceInfoString(childFile),
                                AuditHelper.CreateResourceInfoString(parentFolder));


            auditor.Audit(AuditLevel.Info, FileSystemTask.FileParentRequest, AuditEvent.FolderInfoRequested, msg);
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Gets absolute file path.
        /// </summary>
        ///
        /// <remarks>
        ///     Anwar Javed, 09/13/2013 12:59 PM.
        /// </remarks>
        ///
        /// <param name="fileName">
        ///     Filename of the file.
        /// </param>
        ///
        /// <returns>
        ///     The absolute file path.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public virtual string GetAbsoluteFilePath(string fileName)
        {
            if (!string.IsNullOrWhiteSpace(fileName))
            {
                string filePath = Path.Combine(this.RelativePath, fileName);

                if (this.FileSystem.FileExists(filePath))
                {
                    IVirtualFileItem item = this.FileSystem.GetFile(filePath);

                    if (!item.IsFolder)
                    {
                        return(this.FileSystem.GetAbsolutePath(item));
                    }
                }
            }

            return(string.Empty);
        }
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///     Gets a file.
        /// </summary>
        ///
        /// <remarks>
        ///     Anwar Javed, 09/13/2013 12:59 PM.
        /// </remarks>
        ///
        /// <param name="fileName">
        ///     Filename of the file.
        /// </param>
        ///
        /// <returns>
        ///     The file.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public virtual IVirtualFile GetFile(string fileName)
        {
            if (!string.IsNullOrWhiteSpace(fileName))
            {
                string filePath = Path.Combine(this.RelativePath, fileName);

                if (this.FileSystem.FileExists(filePath))
                {
                    IVirtualFileItem item = this.FileSystem.GetFile(filePath);

                    if (!item.IsFolder)
                    {
                        return((DiskVirtualFile)item);
                    }
                }
            }

            return(null);
        }
    /// <summary>
    /// Creates a <see cref="ResourceLockGuard"/> that encapsulates
    /// a read or write lock for a given file as well as read locks
    /// for all its parent folders.
    /// </summary>
    /// <param name="file">The file to be locked.</param>
    /// <param name="lockType">Whether a read or a write lock is being
    /// required for the <paramref name="file"/>.</param>
    /// <returns>A guard which releases the resource and all folders once it is being disposed.
    /// If locking did not succeed because an element in the chain could not be locked,
    /// the returned <see cref="ResourceLockGuard.IsLockEnabled"/> property
    /// is false.</returns>
    protected virtual ResourceLockGuard RequestChainedLockGuard(IVirtualFileItem file, ResourceLockType lockType)
    {
      List<string> parentFolders = GetResourceLockChain(file);
      
      switch(lockType)
      {
        case ResourceLockType.Read:
          return LockRepository.GetResourceChainLock(file.QualifiedIdentifier, false, parentFolders);
        case ResourceLockType.Write:
          return LockRepository.GetResourceChainLock(file.QualifiedIdentifier, true, parentFolders);
        default:
          throw new ArgumentOutOfRangeException("lockType", "Unknown lock type: " + lockType);
      }

    }
Example #8
0
 /// <summary>
 /// Gets all resources that need to be read-locked (and thus write-protected)
 /// in order to savely access a given file. With hierarchical file systems,
 /// these are usually the parent folders of the file, because deleting,
 /// moving, or renaming the folder would cause file access issues if the
 /// file is still accessed).
 /// </summary>
 /// <param name="file">The currently processed file.</param>
 /// <returns>All resources that need to be write-protected in order to
 /// process the file.</returns>
 protected override List<string> GetResourceLockChain(IVirtualFileItem file) {
     return GetResourceLockChain2(file as ZipFileItem);
 }
Example #9
0
 protected override void WriteFileStreamToFileSystem(IVirtualFileItem fileItem, Stream input) {
     WriteFileStreamToFileSystem2(fileItem as ZipFileItem, input);
 }
Example #10
0
 /// <summary>
 /// Gets authorization claims for a given file resource.
 /// </summary>
 /// <param name="fileItem">The currently processed file.</param>
 /// <returns>The requesting party's permissions for the
 /// submitted <paramref name="fileItem"/>.</returns>
 public FileClaims GetFileClaims(IVirtualFileItem fileItem)
 {
     return(FileClaims.CreatePermissive());
 }
Example #11
0
 /// <summary>
 /// Creates a stream to write the data of a given file to the file system.
 /// This method is invoked by <see cref="FileSystemProviderBase2{TFile,TFolder}.WriteFile(string,System.IO.Stream,bool, long, string)"/> after having
 /// performed access checks.
 /// </summary>
 /// <param name="fileItem">Represents the file to be created or updated.</param>
 /// <param name="input">A stream that provides the file's contents.</param>
 protected override void WriteFileStreamToFileSystem(IVirtualFileItem fileItem, Stream input)
 {
     WriteFileStreamToFileSystem2(fileItem as FileItem, input);
 }
Example #12
0
 /// <summary>
 /// Copies a physical file on the file system from one location to the other. This
 /// method is invoked by <see cref="FileSystemProviderBase2{TFile,TFolder}.CopyFile(string,string)"/>.
 /// </summary>
 /// <param name="sourceFile">The file to be copied.</param>
 /// <param name="targetFile">The designated location of the file copy.</param>
 /// <returns>Either the updated <paramref name="targetFile"/> instance, or a new
 /// <see cref="FileItem"/> instance that represents the created target file.</returns>
 protected override void CopyFileOnFileSystem(IVirtualFileItem sourceFile, IVirtualFileItem targetFile)
 {
     CopyFileOnFileSystem2(sourceFile as FileItem, targetFile as FileItem);
 }
Example #13
0
 /// <summary>
 /// Gets authorization claims for a given file resource.
 /// </summary>
 /// <param name="fileItem">The currently processed file.</param>
 /// <returns>The requesting party's permissions for the
 /// submitted <paramref name="fileItem"/>.</returns>
 public FileClaims GetFileClaims(IVirtualFileItem fileItem)
 {
   return FileClaims.CreatePermissive();
 }
Example #14
0
 /// <summary>
 /// Gets authorization claims for a given file resource.
 /// </summary>
 /// <param name="fileItem">The currently processed file.</param>
 /// <returns>The requesting party's permissions for the
 /// submitted <paramref name="fileItem"/>.</returns>
 public FileClaims GetFileClaims(IVirtualFileItem fileItem)
 {
     throw new NotImplementedException(""); //TODO provide implementation
 }
 /// <summary>
 /// Creates a stream to write the data of a given file to the file system.
 /// This method is invoked by <see cref="WriteFile"/> after having
 /// performed access checks.
 /// </summary>
 /// <param name="fileItem">Represents the file to be created or updated.</param>
 /// <param name="input">A stream that provides the file's contents.</param>
 protected abstract void WriteFileStreamToFileSystem(IVirtualFileItem fileItem, Stream input);
 /// <summary>
 /// Creates a stream to read the data of a given file from the file system.
 /// This method is invoked by <see cref="ReadFileContents"/> after having
 /// performed access checks.
 /// </summary>
 /// <param name="fileItem">Represents the file to be read.</param>
 /// <returns>A stream that provides the file's binary data.</returns>
 protected abstract Stream OpenFileStreamFromFileSystem(IVirtualFileItem fileItem);
 /// <summary>
 /// Copies a physical file on the file system from one location to the other. This
 /// method is invoked by <see cref="CopyFile"/>.
 /// </summary>
 /// <param name="sourceFile">The file to be copied.</param>
 /// <param name="targetFile">The designated location of the file copy.</param>
 protected abstract void CopyFileOnFileSystem(IVirtualFileItem sourceFile, IVirtualFileItem targetFile);
Example #18
0
 /// <summary>
 /// Gets authorization claims for a given file resource.
 /// </summary>
 /// <param name="fileItem">The currently processed file.</param>
 /// <returns>The requesting party's permissions for the
 /// submitted <paramref name="fileItem"/>.</returns>
 public FileClaims GetFileClaims(IVirtualFileItem fileItem)
 {
     return(FileClaimsResolverFunc == null ? null : FileClaimsResolverFunc(fileItem));
 }
Example #19
0
 protected override void MoveFileOnFileSystem(IVirtualFileItem sourceFile, IVirtualFileItem targetFile)
 {
     MoveFileOnFileSystem2(sourceFile as ZipFileItem, targetFile as ZipFileItem);
 }
Example #20
0
        /// <summary>
        /// Audits a request for a given file's meta data (e.g. through <see cref="IFileSystemProvider.GetFileInfo"/>.
        /// Be aware that this might cause verbose audit trails.
        /// </summary>
        public static void AuditFileInfoRequest(IAuditor auditor, FileSystemTask context, IVirtualFileItem file)
        {
            if (!AuditHelper.IsInfoEnabledFor(auditor, context))
            {
                return;
            }

            string msg = String.Format("File information requested:\n{0}", AuditHelper.CreateResourceInfoString(file));

            auditor.Audit(AuditLevel.Info, context, AuditEvent.FileInfoRequested, msg);
        }
 /// <summary>
 /// Tries to acquire a lock for a given file and its parent folders,
 /// and executes the submitted <paramref name="action"/> if the lock
 /// was granted. Otherwise audits a warning.
 /// </summary>
 /// <param name="file">The file to be locked.</param>
 /// <param name="context">The currently performed file system operation.</param>
 /// <param name="lockType">Whether a read or a write lock is required for the
 /// <paramref name="file"/>.</param>
 /// <param name="action">An action that is being executed if the locking
 /// succeeded.</param>
 /// <returns>True if locking succeeded and the <paramref name="action"/> was invoked. False
 /// if the lock was not granted.</returns>
 protected virtual bool LockResourceAndExecute(IVirtualFileItem file, FileSystemTask context, ResourceLockType lockType, Action action)
 {
   using(var guard = RequestChainedLockGuard(file, lockType))
   {
     if (!guard.IsLockEnabled)
     {
       AuditEvent ae = lockType == ResourceLockType.Read
                         ? AuditEvent.FileReadLockDenied
                         : AuditEvent.FileWriteLockDenied;
       AuditHelper.AuditDeniedOperation(Auditor,context, ae, file);
       return false;
     }
     
     action();
     return true;
   }
 }
 /// <summary>
 /// Gets authorization claims for a given file resource.
 /// </summary>
 /// <param name="fileItem">The currently processed file.</param>
 /// <returns>The requesting party's permissions for the
 /// submitted <paramref name="fileItem"/>.</returns>
 public FileClaims GetFileClaims(IVirtualFileItem fileItem)
 {
   throw new NotImplementedException(""); //TODO provide implementation
 }
 /// <summary>
 /// Gets all resources that need to be read-locked (and thus write-protected)
 /// in order to savely access a given file. With hierarchical file systems,
 /// these are usually the parent folders of the file, because deleting,
 /// moving, or renaming the folder would cause file access issues if the
 /// file is still accessed).
 /// </summary>
 /// <param name="file">The currently processed file.</param>
 /// <returns>All resources that need to be write-protected in order to
 /// process the file.</returns>
 protected abstract List<string> GetResourceLockChain(IVirtualFileItem file);
 /// <summary>
 /// Physically deletes a given file on the file system. This method is
 /// being invoked by the <see cref="DeleteFile"/> method.
 /// </summary>
 /// <param name="file">The file to be deleted.</param>
 protected abstract void DeleteFileOnFileSystem(IVirtualFileItem file);
Example #25
0
 protected override void DeleteFileOnFileSystem(IVirtualFileItem file) {
     DeleteFileOnFileSystem2(file as ZipFileItem);
 }
Example #26
0
 /// <summary>
 /// Physically deletes a given file on the file system. This method is
 /// being invoked by the <see cref="FileSystemProviderBase2{TFile,TFolder}.DeleteFile"/> method.
 /// </summary>
 /// <param name="file">The file to be deleted.</param>
 protected override void DeleteFileOnFileSystem(IVirtualFileItem file)
 {
     DeleteFileOnFileSystem2(file as FileItem);
 }
Example #27
0
 protected override void CopyFileOnFileSystem(IVirtualFileItem sourceFile, IVirtualFileItem targetFile) {
     CopyFileOnFileSystem2(sourceFile as ZipFileItem, targetFile as ZipFileItem);
 }
Example #28
0
 /// <summary>
 /// Creates a stream to read the data of a given file from the file system.
 /// This method is invoked by <see cref="FileSystemProviderBase2{TFile,TFolder}.ReadFileContents(string)"/> after having
 /// performed access checks.
 /// </summary>
 /// <param name="fileItem">Represents the file to be read.</param>
 /// <returns>A stream that provides the file's binary data.</returns>
 protected override Stream OpenFileStreamFromFileSystem(IVirtualFileItem fileItem)
 {
     return(OpenFileStreamFromFileSystem2(fileItem as FileItem));
 }
Example #29
0
 /// <summary>
 /// Creates a stream to read the data of a given file from the file system.
 /// This method is invoked by <see cref="FileSystemProviderBase2{TFile,TFolder}.ReadFileContents(string)"/> after having
 /// performed access checks.
 /// </summary>
 /// <param name="fileItem">Represents the file to be read.</param>
 /// <returns>A stream that provides the file's binary data.</returns>
 protected override Stream OpenFileStreamFromFileSystem(IVirtualFileItem fileItem) {
     return OpenFileStreamFromFileSystem2(fileItem as ZipFileItem);
 }
Example #30
0
 /// <summary>
 /// Gets all resources that need to be read-locked (and thus write-protected)
 /// in order to savely access a given file. With hierarchical file systems,
 /// these are usually the parent folders of the file, because deleting,
 /// moving, or renaming the folder would cause file access issues if the
 /// file is still accessed).
 /// </summary>
 /// <param name="file">The currently processed file.</param>
 /// <returns>All resources that need to be write-protected in order to
 /// process the file.</returns>
 protected override List <string> GetResourceLockChain(IVirtualFileItem file)
 {
     return(GetResourceLockChain2(file as FileItem));
 }
 /// <summary>
 /// Moves a physical file on the file system from one location to the other. This
 /// method is invoked by <see cref="FileSystemProviderBase2{TFile,TFolder}.MoveFile(string,string)"/>.
 /// </summary>
 /// <param name="sourceFile">The file to be moved.</param>
 /// <param name="targetFile">The designated new location of the file.</param>
 /// <returns>Either the updated <paramref name="targetFile"/> instance, or a new
 /// <see cref="FileItem"/> instance that represents the created target file.</returns>
 protected override void MoveFileOnFileSystem(IVirtualFileItem sourceFile, IVirtualFileItem targetFile) {
     MoveFileOnFileSystem2(sourceFile as FileItem,targetFile as FileItem);
 }