Skip to content

Commit 35377cf

Browse files
authored
#Fix: ContentType was forced to image/jpeg for every Multimedia and Blob Upload. (#385)
1 parent bf0f443 commit 35377cf

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed

dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using TZ4Net;
1919
using GxClasses.Helpers;
2020
using System.Net;
21+
using GeneXus.Mime;
2122
#endif
2223
using GeneXus.Web.Security;
2324

@@ -5585,7 +5586,33 @@ public static long GetFileSize(string imageFile)
55855586
public class StorageUtils
55865587
{
55875588
public const string DELIMITER = "/";
5589+
public static string DEFAULT_TMP_CONTENT_TYPE = "image/jpeg";
5590+
public static string DEFAULT_CONTENT_TYPE = "application/octet-stream";
55885591

5592+
public static bool TryGetContentType(string fileName, out string mimeType, string defaultValue = null)
5593+
{
5594+
mimeType = defaultValue;
5595+
string extension = Path.GetExtension(fileName);
5596+
if (!string.IsNullOrEmpty(extension))
5597+
{
5598+
if (fileName.EndsWith(".tmp"))
5599+
{
5600+
mimeType = DEFAULT_TMP_CONTENT_TYPE;
5601+
}
5602+
else
5603+
{
5604+
try
5605+
{
5606+
mimeType = MimeMapping.GetMimeMapping(fileName);
5607+
}
5608+
catch (Exception)
5609+
{
5610+
}
5611+
}
5612+
}
5613+
return mimeType != null;
5614+
5615+
}
55895616
public static string EncodeUrl(string objectName)
55905617
{
55915618
if (!Uri.IsWellFormedUriString(objectName, UriKind.RelativeOrAbsolute))

dotnet/src/dotnetframework/Providers/Storage/GXAmazonS3/ExternalProviderS3.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,13 @@ public string Upload(string fileName, Stream stream, GxFileType destFileType)
279279
BucketName = Bucket,
280280
Key = fileName,
281281
InputStream = stream,
282-
CannedACL = GetCannedACL(destFileType)
282+
CannedACL = GetCannedACL(destFileType),
283+
283284
};
284-
if (Path.GetExtension(fileName).Equals(".tmp"))
285-
objectRequest.ContentType = "image/jpeg";
285+
if (StorageUtils.TryGetContentType(fileName, out string mimeType))
286+
{
287+
objectRequest.ContentType = mimeType;
288+
}
286289
PutObjectResponse result = PutObject(objectRequest);
287290
return Get(fileName, destFileType);
288291
}
@@ -301,8 +304,15 @@ public string Copy(string url, string newName, string tableName, string fieldNam
301304
SourceKey = url,
302305
DestinationBucket = Bucket,
303306
DestinationKey = resourceKey,
304-
CannedACL = GetCannedACL(destFileType)
307+
CannedACL = GetCannedACL(destFileType),
308+
MetadataDirective = S3MetadataDirective.REPLACE
305309
};
310+
311+
if (StorageUtils.TryGetContentType(newName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE))
312+
{
313+
request.ContentType = mimeType;
314+
}
315+
306316
AddObjectMetadata(request.Metadata, tableName, fieldName, resourceKey);
307317
CopyObject(request);
308318

@@ -322,6 +332,12 @@ public string Save(Stream fileStream, string fileName, string tableName, string
322332
InputStream = fileStream,
323333
CannedACL = GetCannedACL(destFileType)
324334
};
335+
336+
if (StorageUtils.TryGetContentType(fileName, out string mimeType))
337+
{
338+
objectRequest.ContentType = mimeType;
339+
}
340+
325341
AddObjectMetadata(objectRequest.Metadata, tableName, fieldName, resourceKey);
326342
PutObjectResponse result = PutObject(objectRequest);
327343

dotnet/src/dotnetframework/Providers/Storage/GXAzureStorage/AzureStorageExternalProvider.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ public string Copy(string objectName, GxFileType sourceFileType, string newName,
146146
public string Upload(string fileName, Stream stream, GxFileType fileType)
147147
{
148148
CloudBlockBlob blob = GetCloudBlockBlob(fileName, fileType);
149-
if (Path.GetExtension(fileName).Equals(".tmp"))
150-
blob.Properties.ContentType = "image/jpeg";
151-
else
152-
blob.Properties.ContentType = MimeMapping.GetMimeMapping(fileName);
149+
if (StorageUtils.TryGetContentType(fileName, out string mimeType))
150+
{
151+
blob.Properties.ContentType = mimeType;
152+
}
153153

154154
blob.UploadFromStreamAsync(stream).GetAwaiter().GetResult();
155155
return GetURL(blob, fileType);
@@ -166,7 +166,14 @@ public string Copy(string sourceUrl, string newName, string tableName, string fi
166166
targetBlob.Metadata["Field"] = fieldName;
167167
targetBlob.Metadata["KeyValue"] = StorageUtils.EncodeUrl(newName);
168168

169+
if (StorageUtils.TryGetContentType(newName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE))
170+
{
171+
targetBlob.Properties.ContentType = mimeType;
172+
}
173+
169174
targetBlob.StartCopyAsync(sourceBlob).GetAwaiter().GetResult();
175+
targetBlob.SetPropertiesAsync().GetAwaiter().GetResult(); //Required to apply new object metadata
176+
170177
return GetURL(targetBlob, fileType);
171178
}
172179
return string.Empty;

dotnet/src/dotnetframework/Providers/Storage/GXGoogleCloud/ExternalProviderGoogle.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ public string Upload(string fileName, Stream stream, GxFileType fileType)
129129
obj.Name = fileName;
130130
obj.Bucket = Bucket;
131131

132-
if (Path.GetExtension(fileName).Equals(".tmp"))
133-
obj.ContentType = "image/jpeg";
134-
else
135-
obj.ContentType = MimeMapping.GetMimeMapping(fileName);
136-
137-
Client.UploadObject(obj, stream, GetUploadOptions(fileType));
132+
if (StorageUtils.TryGetContentType(fileName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE))
133+
{
134+
obj.ContentType = mimeType;
135+
}
136+
137+
Client.UploadObject(obj, stream, GetUploadOptions(fileType));
138138
return StorageUri + StorageUtils.EncodeUrl(fileName);
139139
}
140140

@@ -175,7 +175,8 @@ public string Upload(string localFile, string objectName, GxFileType fileType)
175175
{
176176
using (FileStream stream = new FileStream(localFile, FileMode.Open))
177177
{
178-
Google.Apis.Storage.v1.Data.Object obj = Client.UploadObject(Bucket, objectName, "application/octet-stream", stream, GetUploadOptions(fileType));
178+
StorageUtils.TryGetContentType(objectName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE);
179+
Google.Apis.Storage.v1.Data.Object obj = Client.UploadObject(Bucket, objectName, mimeType, stream, GetUploadOptions(fileType));
179180
return obj.MediaLink;
180181
}
181182
}
@@ -246,8 +247,12 @@ public string Copy(string url, string newName, string tableName, string fieldNam
246247

247248
Google.Apis.Storage.v1.Data.Object obj = Client.CopyObject(Bucket, url, Bucket, newName, GetCopyOptions(fileType));
248249
obj.Metadata = CreateObjectMetadata(tableName, fieldName, newName);
249-
Client.UpdateObject(obj);
250-
return GetURL(newName, fileType, 0);
250+
if (StorageUtils.TryGetContentType(newName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE))
251+
{
252+
obj.ContentType = mimeType;
253+
}
254+
Client.UpdateObject(obj);
255+
return GetURL(newName, fileType, 0);
251256
}
252257

253258
public Stream GetStream(string objectName, GxFileType fileType)

0 commit comments

Comments
 (0)