소스 검색

Bug 1604: Configurable mapping from file extension to Content-Type

https://winscp.net/tracker/1604

Source commit: d281df33649c7f2ebe228cbbf98a9fe01316621d
Martin Prikryl 7 년 전
부모
커밋
c3a1f6d6fa
4개의 변경된 파일44개의 추가작업 그리고 2개의 파일을 삭제
  1. 38 0
      source/core/Configuration.cpp
  2. 4 0
      source/core/Configuration.h
  3. 1 1
      source/core/S3FileSystem.cpp
  4. 1 1
      source/core/WebDAVFileSystem.cpp

+ 38 - 0
source/core/Configuration.cpp

@@ -12,6 +12,7 @@
 #include "Interface.h"
 #include "CoreMain.h"
 #include "Security.h"
+#include "FileMasks.h"
 #include <shlobj.h>
 #include <System.IOUtils.hpp>
 #include <System.StrUtils.hpp>
@@ -103,6 +104,7 @@ void __fastcall TConfiguration::Default()
   FExternalIpAddress = L"";
   FTryFtpWhenSshFails = true;
   FParallelDurationThreshold = 10;
+  FMimeTypes = UnicodeString();
   CollectUsage = FDefaultCollectUsage;
 
   FLogging = false;
@@ -223,6 +225,7 @@ UnicodeString __fastcall TConfiguration::PropertyToKey(const UnicodeString & Pro
     KEY(String,   ExternalIpAddress); \
     KEY(Bool,     TryFtpWhenSshFails); \
     KEY(Integer,  ParallelDurationThreshold); \
+    KEY(String,   MimeTypes); \
     KEY(Bool,     CollectUsage); \
   ); \
   BLOCK(L"Logging", CANCREATE, \
@@ -1055,6 +1058,36 @@ UnicodeString __fastcall TConfiguration::GetFileInfoString(const UnicodeString K
   return GetFileFileInfoString(Key, L"");
 }
 //---------------------------------------------------------------------------
+UnicodeString __fastcall TConfiguration::GetFileMimeType(const UnicodeString & FileName)
+{
+  UnicodeString Result;
+  bool Found = false;
+
+  if (!MimeTypes.IsEmpty())
+  {
+    UnicodeString FileNameOnly = ExtractFileName(FileName);
+    UnicodeString AMimeTypes = MimeTypes;
+    while (!Found && !AMimeTypes.IsEmpty())
+    {
+      UnicodeString Token = CutToChar(AMimeTypes, L',', true);
+      UnicodeString MaskStr = CutToChar(Token, L'=', true);
+      TFileMasks Mask(MaskStr);
+      if (Mask.Matches(FileNameOnly))
+      {
+        Result = Token.Trim();
+        Found = true;
+      }
+    }
+  }
+
+  if (!Found) // allow an override to "no" Content-Type
+  {
+    Result = ::GetFileMimeType(FileName);
+  }
+
+  return Result;
+}
+//---------------------------------------------------------------------------
 UnicodeString __fastcall TConfiguration::GetRegistryStorageKey()
 {
   return GetRegistryKey();
@@ -1425,6 +1458,11 @@ void __fastcall TConfiguration::SetExternalIpAddress(UnicodeString value)
   SET_CONFIG_PROPERTY(ExternalIpAddress);
 }
 //---------------------------------------------------------------------
+void __fastcall TConfiguration::SetMimeTypes(UnicodeString value)
+{
+  SET_CONFIG_PROPERTY(MimeTypes);
+}
+//---------------------------------------------------------------------
 void __fastcall TConfiguration::SetTryFtpWhenSshFails(bool value)
 {
   SET_CONFIG_PROPERTY(TryFtpWhenSshFails);

+ 4 - 0
source/core/Configuration.h

@@ -72,6 +72,7 @@ private:
   bool FTryFtpWhenSshFails;
   int FParallelDurationThreshold;
   bool FScripting;
+  UnicodeString FMimeTypes;
 
   bool FDisablePasswordStoring;
   bool FForceBanners;
@@ -136,6 +137,7 @@ private:
   void __fastcall SetExternalIpAddress(UnicodeString value);
   void __fastcall SetTryFtpWhenSshFails(bool value);
   void __fastcall SetParallelDurationThreshold(int value);
+  void __fastcall SetMimeTypes(UnicodeString value);
   bool __fastcall GetCollectUsage();
   void __fastcall SetCollectUsage(bool value);
   bool __fastcall GetIsUnofficial();
@@ -237,6 +239,7 @@ public:
   virtual RawByteString __fastcall StronglyRecryptPassword(RawByteString Password, UnicodeString Key);
   UnicodeString __fastcall GetFileDescription(const UnicodeString & FileName);
   UnicodeString __fastcall GetFileVersion(const UnicodeString & FileName);
+  UnicodeString __fastcall GetFileMimeType(const UnicodeString & FileName);
 
   TStoredSessionList * __fastcall SelectFilezillaSessionsForImport(
     TStoredSessionList * Sessions, UnicodeString & Error);
@@ -296,6 +299,7 @@ public:
   __property UnicodeString ExternalIpAddress = { read = FExternalIpAddress, write = SetExternalIpAddress };
   __property bool TryFtpWhenSshFails = { read = FTryFtpWhenSshFails, write = SetTryFtpWhenSshFails };
   __property int ParallelDurationThreshold = { read = FParallelDurationThreshold, write = SetParallelDurationThreshold };
+  __property UnicodeString MimeTypes = { read = FMimeTypes, write = SetMimeTypes };
 
   __property UnicodeString TimeFormat = { read = GetTimeFormat };
   __property TStorage Storage  = { read=GetStorage };

+ 1 - 1
source/core/S3FileSystem.cpp

@@ -1314,7 +1314,7 @@ void __fastcall TS3FileSystem::Source(
 
   TLibS3BucketContext BucketContext = GetBucketContext(BucketName);
 
-  UTF8String ContentType = UTF8String(GetFileMimeType(Handle.FileName));
+  UTF8String ContentType = UTF8String(FTerminal->Configuration->GetFileMimeType(Handle.FileName));
   S3PutProperties PutProperties =
     {
       (ContentType.IsEmpty() ? NULL : ContentType.c_str()),

+ 1 - 1
source/core/WebDAVFileSystem.cpp

@@ -1272,7 +1272,7 @@ void __fastcall TWebDAVFileSystem::Source(
     // (not really true as we do not support changing file name on overwrite dialog)
     Action.Destination(DestFullName);
 
-    FUploadMimeType = GetFileMimeType(DestFileName);
+    FUploadMimeType = Configuration->GetFileMimeType(DestFileName);
 
     FILE_OPERATION_LOOP_BEGIN
     {