private string?ProcessFile(ContentPropertyFile file, Guid cuid, Guid puid) { // process the file // no file, invalid file, reject change if (UploadFileTypeValidator.IsValidFileExtension(file.FileName, _contentSettings) == false) { return(null); } // get the filepath // in case we are using the old path scheme, try to re-use numbers (bah...) var filepath = _mediaFileManager.GetMediaPath(file.FileName, cuid, puid); // fs-relative path using (FileStream filestream = File.OpenRead(file.TempFilePath)) { // TODO: Here it would make sense to do the auto-fill properties stuff but the API doesn't allow us to do that right // since we'd need to be able to return values for other properties from these methods _mediaFileManager.FileSystem.AddFile(filepath, filestream, true); // must overwrite! } return(filepath); }
public void Can_Delete_MediaFiles() { MediaFileManager mediaFileManager = GetRequiredService <MediaFileManager>(); var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("test")); string virtualPath = mediaFileManager.GetMediaPath("file.txt", Guid.NewGuid(), Guid.NewGuid()); mediaFileManager.FileSystem.AddFile(virtualPath, memoryStream); // ~/media/1234/file.txt exists IHostingEnvironment hostingEnvironment = GetRequiredService <IHostingEnvironment>(); string physPath = hostingEnvironment.MapPathWebRoot(Path.Combine("media", virtualPath)); Assert.IsTrue(File.Exists(physPath)); // ~/media/1234/file.txt is gone mediaFileManager.DeleteMediaFiles(new[] { virtualPath }); Assert.IsFalse(File.Exists(physPath)); IMediaPathScheme scheme = GetRequiredService <IMediaPathScheme>(); if (scheme is UniqueMediaPathScheme) { // ~/media/1234 is *not* gone physPath = Path.GetDirectoryName(physPath); Assert.IsTrue(Directory.Exists(physPath)); } else { // ~/media/1234 is gone physPath = Path.GetDirectoryName(physPath); Assert.IsFalse(Directory.Exists(physPath)); } // ~/media exists physPath = Path.GetDirectoryName(physPath); Assert.IsTrue(Directory.Exists(physPath)); }