2017-09-11 67 views
1

我試圖創建一個自定義ImageFilter,它要求我暫時將映像寫入磁盤,因爲我使用的是僅以FileInfo對象爲參數的第三方庫。我希望我可以使用IStorageProvider輕鬆地寫入和獲取文件,但似乎無法找到將IStorageFile轉換爲FileInfo或獲取當前租戶的媒體文件夾的完整路徑以自行檢索文件的方法。如何從Orchard Media文件夾中獲取FileInfo對象?

public class CustomFilter: IImageFilterProvider { 

    public void ApplyFilter(FilterContext context) 
    { 
     if (context.Media.CanSeek) 
     { 
      context.Media.Seek(0, SeekOrigin.Begin); 
     } 

     // Save temporary image 
     var fileName = context.FilePath.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); 

     if (!string.IsNullOrEmpty(fileName)) 
     { 
      var tempFilePath = string.Format("tmp/tmp_{0}", fileName); 
      _storageProvider.TrySaveStream(tempFilePath, context.Media); 

      IStorageFile temp = _storageProvider.GetFile(tempFilePath); 
      FileInfo tempFile = ??? 

      // Do all kinds of things with the temporary file 

      // Convert back to Stream and pass along 
      context.Media = tempFile.OpenRead(); 
     } 
    }  
} 

FileSystemStorageProvider做一噸繁重的構建路徑Media文件夾所以這是一個恥辱,他們不供公衆查閱。我寧願不必複製所有的初始化代碼。有沒有簡單的方法來直接訪問媒體文件夾中的文件?

回答

1

我不使用多租戶,所以請原諒我,如果這是不準確的,但是這是我使用的方法,用於從獲取完整的存儲路徑,然後選擇的FileInfo對象:

_storagePath = HostingEnvironment.IsHosted 
    ? HostingEnvironment.MapPath("~/Media/") ?? "" 
    : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Media"); 

files = Directory.GetFiles(_storagePath, "*", SearchOption.AllDirectories).AsEnumerable().Select(f => new FileInfo(f)); 

可以,當然,請使用子文件夾名稱的Path.Combine或GetFiles調用的Where子句過濾文件列表。

這幾乎完全是FileSystemStorageProvider所使用的,但是我沒有需要其他調用,因爲它沒有弄清楚_storagePath應該是什麼。

簡而言之,是的,您可能需要重新實現任務所需的FileSystemStorageProvider的所有私有函數。但是你可能不需要所有這些。

+0

這就是我最終做的。我需要一些額外的技巧來將租戶的名稱添加到路徑中,並且它不是很漂亮,但它至少適用於我的目的。 – Lawyerson

0

我也在努力解決類似的問題,我可以說IStorageProvider的東西是非常受限制的。

在查看FileSystemStorageFile的代碼時可以看到這個。該類已經使用FileInfo來返回數據,但結構本身不可訪問,其他代碼基於此。因此,你將不得不從頭開始重新實現一切(自己實現IStorageProvider)。最簡單的方法是簡單地調用

FileInfo fileInfo = new FileInfo(tempFilePath);

,但在沒有基於文件系統的存儲提供商用於像AzureBlobStorageProvider這將打破設置。

此任務的正確方法是讓您的手變髒並擴展存儲提供程序接口並更新基於它的所有代碼。但據我記得這裏的問題是,你還需要更新Azure的東西,然後事情變得非常混亂。由於這個事實,當我試圖在我的項目上做這些沉重的事情時,我放棄了這種做法。

相關問題