/// <summary> /// Deletes a file. /// </summary> /// <param name="path">The filesystem-relative path to the file.</param> public void DeleteFile(string path) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullPath = GetFullPath(path); if (File.Exists(fullPath) == false) { return; } WithRetry(() => File.Delete(fullPath)); } catch (FileNotFoundException ex) { LogHelper.Info <PhysicalFileSystemWithSecureFormsUploads>(string.Format("DeleteFile failed with FileNotFoundException: {0}", ex.InnerException)); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
/// <summary> /// Gets files in a directory. /// </summary> /// <param name="path">The filesystem-relative path of the directory.</param> /// <param name="filter">A filter.</param> /// <returns>The filesystem-relative path to the matching files in the directory.</returns> /// <remarks>Filesystem-relative paths use forward-slashes as directory separators.</remarks> public IEnumerable <string> GetFiles(string path, string filter) { ImpersonatorWrapper impersonator = null; var fullPath = GetFullPath(path); try { impersonator = BuildImpersonationContext(path); if (Directory.Exists(fullPath)) { return(Directory.EnumerateFiles(fullPath, filter).Select(GetRelativePath)); } } catch (UnauthorizedAccessException ex) { LogHelper.Error <PhysicalFileSystemWithSecureFormsUploads>("Not authorized to get directories", ex); } catch (DirectoryNotFoundException ex) { LogHelper.Error <PhysicalFileSystemWithSecureFormsUploads>("Directory not found", ex); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } return(Enumerable.Empty <string>()); }
/// <summary> /// Deletes a directory. /// </summary> /// <param name="path">The filesystem-relative path of the directory.</param> /// <param name="recursive">A value indicating whether to recursively delete sub-directories.</param> public void DeleteDirectory(string path, bool recursive) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullPath = GetFullPath(path); if (Directory.Exists(fullPath) == false) { return; } WithRetry(() => Directory.Delete(fullPath, recursive)); } catch (DirectoryNotFoundException ex) { LogHelper.Error <PhysicalFileSystemWithSecureFormsUploads>("Directory not found", ex); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
protected virtual void EnsureDirectory(string path) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); path = GetFullPath(path); Directory.CreateDirectory(path); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
/// <summary> /// Opens a file. /// </summary> /// <param name="path">The filesystem-relative path to the file.</param> /// <returns></returns> public Stream OpenFile(string path) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullPath = GetFullPath(path); return(File.OpenRead(fullPath)); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
public void AddFile(string path, string physicalPath, bool overrideIfExists = true, bool copy = false) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullPath = GetFullPath(path); if (File.Exists(fullPath)) { if (overrideIfExists == false) { throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", path)); } WithRetry(() => File.Delete(fullPath)); } var directory = Path.GetDirectoryName(fullPath); if (directory == null) { throw new InvalidOperationException("Could not get directory."); } Directory.CreateDirectory(directory); // ensure it exists if (copy) { WithRetry(() => File.Copy(physicalPath, fullPath)); } else { WithRetry(() => File.Move(physicalPath, fullPath)); } } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
/// <summary> /// Gets a value indicating whether a file exists. /// </summary> /// <param name="path">The filesystem-relative path to the file.</param> /// <returns>A value indicating whether the file exists.</returns> public bool FileExists(string path) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullpath = GetFullPath(path); return(File.Exists(fullpath)); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
/// <summary> /// Saves a file. /// </summary> /// <param name="path">The filesystem-relative path of the file.</param> /// <param name="stream">A stream containing the file data.</param> /// <param name="overrideExisting">A value indicating whether to override the existing file, if any.</param> /// <remarks>If a file exists and <paramref name="overrideExisting"/> is false, an exception is thrown.</remarks> public void AddFile(string path, Stream stream, bool overrideExisting) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullPath = GetFullPath(path); var exists = File.Exists(fullPath); if (exists && overrideExisting == false) { throw new InvalidOperationException(string.Format("A file at path '{0}' already exists", path)); } var directory = Path.GetDirectoryName(fullPath); if (directory == null) { throw new InvalidOperationException("Could not get directory."); } Directory.CreateDirectory(directory); // ensure it exists // if can seek, be safe and go back to start, else... // hope that the stream hasn't been read already if (stream.CanSeek) { stream.Seek(0, SeekOrigin.Begin); } using (var destination = (Stream)File.Create(fullPath)) { stream.CopyTo(destination); } } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
/// <summary> /// Gets the size of a file. /// </summary> /// <param name="path">The filesystem-relative path to the file.</param> /// <returns>The file of the size, in bytes.</returns> /// <remarks>If the file does not exist, returns -1.</remarks> public long GetSize(string path) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullPath = GetFullPath(path); var file = new FileInfo(fullPath); return(file.Exists ? file.Length : -1); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
/// <summary> /// Gets the created date of a directory or file. /// </summary> /// <param name="path">The filesystem-relative path to the directory or the file.</param> /// <returns>The created date of the directory or the file.</returns> public DateTimeOffset GetCreated(string path) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullpath = GetFullPath(path); return(DirectoryExists(fullpath) ? Directory.GetCreationTimeUtc(fullpath) : File.GetCreationTimeUtc(fullpath)); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }
/// <summary> /// Gets the last-modified date of a directory or file. /// </summary> /// <param name="path">The filesystem-relative path to the directory or the file.</param> /// <returns>The last modified date of the directory or the file.</returns> public DateTimeOffset GetLastModified(string path) { ImpersonatorWrapper impersonator = null; try { impersonator = BuildImpersonationContext(path); var fullpath = GetFullPath(path); return(DirectoryExists(fullpath) ? new DirectoryInfo(fullpath).LastWriteTimeUtc : new FileInfo(fullpath).LastWriteTimeUtc); } finally { if (impersonator != null) { impersonator.UndoUserImpersonation(); } } }