Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using TZ4Net;
using GxClasses.Helpers;
using System.Net;
using GeneXus.Mime;
#endif
using GeneXus.Web.Security;

Expand Down Expand Up @@ -5585,7 +5586,33 @@ public static long GetFileSize(string imageFile)
public class StorageUtils
{
public const string DELIMITER = "/";
public static string DEFAULT_TMP_CONTENT_TYPE = "image/jpeg";
public static string DEFAULT_CONTENT_TYPE = "application/octet-stream";

public static bool TryGetContentType(string fileName, out string mimeType, string defaultValue = null)
{
mimeType = defaultValue;
string extension = Path.GetExtension(fileName);
if (!string.IsNullOrEmpty(extension))
{
if (fileName.EndsWith(".tmp"))
{
mimeType = DEFAULT_TMP_CONTENT_TYPE;
}
else
{
try
{
mimeType = MimeMapping.GetMimeMapping(fileName);
}
catch (Exception)
{
}
}
}
return mimeType != null;

}
public static string EncodeUrl(string objectName)
{
if (!Uri.IsWellFormedUriString(objectName, UriKind.RelativeOrAbsolute))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,13 @@ public string Upload(string fileName, Stream stream, GxFileType destFileType)
BucketName = Bucket,
Key = fileName,
InputStream = stream,
CannedACL = GetCannedACL(destFileType)
CannedACL = GetCannedACL(destFileType),

};
if (Path.GetExtension(fileName).Equals(".tmp"))
objectRequest.ContentType = "image/jpeg";
if (StorageUtils.TryGetContentType(fileName, out string mimeType))
{
objectRequest.ContentType = mimeType;
}
PutObjectResponse result = PutObject(objectRequest);
return Get(fileName, destFileType);
}
Expand All @@ -301,8 +304,15 @@ public string Copy(string url, string newName, string tableName, string fieldNam
SourceKey = url,
DestinationBucket = Bucket,
DestinationKey = resourceKey,
CannedACL = GetCannedACL(destFileType)
CannedACL = GetCannedACL(destFileType),
MetadataDirective = S3MetadataDirective.REPLACE
};

if (StorageUtils.TryGetContentType(newName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE))
{
request.ContentType = mimeType;
}

AddObjectMetadata(request.Metadata, tableName, fieldName, resourceKey);
CopyObject(request);

Expand All @@ -322,6 +332,12 @@ public string Save(Stream fileStream, string fileName, string tableName, string
InputStream = fileStream,
CannedACL = GetCannedACL(destFileType)
};

if (StorageUtils.TryGetContentType(fileName, out string mimeType))
{
objectRequest.ContentType = mimeType;
}

AddObjectMetadata(objectRequest.Metadata, tableName, fieldName, resourceKey);
PutObjectResponse result = PutObject(objectRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ public string Copy(string objectName, GxFileType sourceFileType, string newName,
public string Upload(string fileName, Stream stream, GxFileType fileType)
{
CloudBlockBlob blob = GetCloudBlockBlob(fileName, fileType);
if (Path.GetExtension(fileName).Equals(".tmp"))
blob.Properties.ContentType = "image/jpeg";
else
blob.Properties.ContentType = MimeMapping.GetMimeMapping(fileName);
if (StorageUtils.TryGetContentType(fileName, out string mimeType))
{
blob.Properties.ContentType = mimeType;
}

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

if (StorageUtils.TryGetContentType(newName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE))
{
targetBlob.Properties.ContentType = mimeType;
}

targetBlob.StartCopyAsync(sourceBlob).GetAwaiter().GetResult();
targetBlob.SetPropertiesAsync().GetAwaiter().GetResult(); //Required to apply new object metadata

return GetURL(targetBlob, fileType);
}
return string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ public string Upload(string fileName, Stream stream, GxFileType fileType)
obj.Name = fileName;
obj.Bucket = Bucket;

if (Path.GetExtension(fileName).Equals(".tmp"))
obj.ContentType = "image/jpeg";
else
obj.ContentType = MimeMapping.GetMimeMapping(fileName);
Client.UploadObject(obj, stream, GetUploadOptions(fileType));
if (StorageUtils.TryGetContentType(fileName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE))
{
obj.ContentType = mimeType;
}

Client.UploadObject(obj, stream, GetUploadOptions(fileType));
return StorageUri + StorageUtils.EncodeUrl(fileName);
}

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

Google.Apis.Storage.v1.Data.Object obj = Client.CopyObject(Bucket, url, Bucket, newName, GetCopyOptions(fileType));
obj.Metadata = CreateObjectMetadata(tableName, fieldName, newName);
Client.UpdateObject(obj);
return GetURL(newName, fileType, 0);
if (StorageUtils.TryGetContentType(newName, out string mimeType, StorageUtils.DEFAULT_CONTENT_TYPE))
{
obj.ContentType = mimeType;
}
Client.UpdateObject(obj);
return GetURL(newName, fileType, 0);
}

public Stream GetStream(string objectName, GxFileType fileType)
Expand Down