Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added "protected" property to the WiserItemFile model and added option to load protected files and images on 'friendly' URL's #742

Merged
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
2 changes: 2 additions & 0 deletions GeeksCoreLibrary/Core/Models/WiserItemFileModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class WiserItemFileModel

public string AddedBy { get; set; }

public bool Protected { get; set; }

/// <summary>
/// Gets or sets the object for storing extra data, such as alt texts in multiple languages for images.
/// </summary>
Expand Down
8 changes: 5 additions & 3 deletions GeeksCoreLibrary/Core/Services/WiserItemsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@
throw;
}
}
catch(Exception exp)

Check warning on line 1327 in GeeksCoreLibrary/Core/Services/WiserItemsService.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'exp' is declared but never used
{
if (createNewTransaction && !alreadyHadTransaction) await databaseConnection.RollbackTransactionAsync();
throw;
Expand Down Expand Up @@ -3377,6 +3377,7 @@
databaseConnection.AddParameter("extension", wiserItemFile.Extension);
databaseConnection.AddParameter("title", wiserItemFile.Title);
databaseConnection.AddParameter("propertyName", wiserItemFile.PropertyName);
databaseConnection.AddParameter("protected", wiserItemFile.Protected);
databaseConnection.AddParameter("extraData", wiserItemFile.ExtraData == null ? null : JsonConvert.SerializeObject(wiserItemFile.ExtraData));
databaseConnection.AddParameter("username", username);
databaseConnection.AddParameter("userId", userId);
Expand All @@ -3387,8 +3388,8 @@
SET @_username = ?username;
SET @_userId = ?userId;
SET @saveHistory = ?saveHistoryGcl;
INSERT IGNORE INTO {(linkType > 0 ? linkTablePrefix : tablePrefix)}{WiserTableNames.WiserItemFile} (item_id, content_type, content, content_url, width, height, file_name, extension, added_by, title, property_name, itemlink_id, extra_data)
VALUES (?itemId, ?contentType, ?content, ?contentUrl, ?width, ?height, ?fileName, ?extension, ?username, ?title, ?propertyName, ?itemLinkId, ?extraData);
INSERT IGNORE INTO {(linkType > 0 ? linkTablePrefix : tablePrefix)}{WiserTableNames.WiserItemFile} (item_id, content_type, content, content_url, width, height, file_name, extension, added_by, title, property_name, protected, itemlink_id, extra_data)
VALUES (?itemId, ?contentType, ?content, ?contentUrl, ?width, ?height, ?fileName, ?extension, ?username, ?title, ?propertyName, ?protected, ?itemLinkId, ?extraData);
SELECT LAST_INSERT_ID();", true);

return Convert.ToUInt64(addItemFileResult.Rows[0][0]);
Expand Down Expand Up @@ -3445,7 +3446,7 @@

databaseConnection.AddParameter("Ids", String.Join(",", ids));
var queryResult = await databaseConnection.GetAsync($@"
SELECT `id`, `item_id`, `content_type`, `content`, `content_url`, `width`, `height`, `file_name`, `extension`, `added_on`, `added_by`, `title`, `property_name`, `itemlink_id`, extra_data
SELECT `id`, `item_id`, `content_type`, `content`, `content_url`, `width`, `height`, `file_name`, `extension`, `added_on`, `added_by`, `title`, `property_name`, `protected`, `itemlink_id`, extra_data
FROM {tablePrefix}{WiserTableNames.WiserItemFile}
WHERE {columnName} IN ({String.Join(",", ids)})
{propertyNameClause}", true);
Expand Down Expand Up @@ -3665,7 +3666,7 @@
}

/// <inheritcDoc />
public async Task<string> ReplaceRelativeImagesToAbsoluteAsync(string input, string imagesDomain)

Check warning on line 3669 in GeeksCoreLibrary/Core/Services/WiserItemsService.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var output = input;

Expand Down Expand Up @@ -4432,6 +4433,7 @@
Extension = dataRow.Field<string>("extension"),
Title = dataRow.Field<string>("title"),
PropertyName = dataRow.Field<string>("property_name"),
Protected = dataRow.Field<bool>("protected"),
ExtraData = dataRow.IsNull("extra_data") ? null : JsonConvert.DeserializeObject<WiserItemFileExtraDataModel>(dataRow.Field<string>("extra_data")!)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public async Task<IActionResult> WiserItemImage([FromQuery] ulong itemId, [FromQ
[FromQuery] string fileType = null, [FromQuery] string type = null,
[FromQuery] string encryptedId = null)
{
if (itemId == 0 || String.IsNullOrWhiteSpace(propertyName))
if ((itemId == 0 && String.IsNullOrEmpty(encryptedId)) || String.IsNullOrWhiteSpace(propertyName))
{
return NotFound();
}

// Also check if fileName is empty when fileType is "name"
if (String.Equals(fileType, "name", StringComparison.OrdinalIgnoreCase) && String.IsNullOrWhiteSpace(fileName))
{
Expand Down Expand Up @@ -82,7 +83,7 @@ public async Task<IActionResult> WiserItemFile([FromQuery] ulong itemId, [FromQu
[FromQuery] string filename, [FromQuery] int fileNumber = 1, [FromQuery] string encryptedId = null,
[FromQuery] string type = null, [FromQuery] string fileType = null)
{
if (itemId == 0 || String.IsNullOrWhiteSpace(propertyName))
if ((itemId == 0 && String.IsNullOrEmpty(encryptedId)) || String.IsNullOrWhiteSpace(propertyName))
{
return NotFound();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,28 @@ private void HandleRewrites(HttpContext context, string path, QueryString queryS
var matchResult = urlRegex.Match(path);
if (!matchResult.Success)
{
urlRegex = new Regex(@"(?:file\/wiser[0-9]?\/)(?:(?<type>[^\/]+)\/)?(?<itemId>\d+)(?:\/(?<fileType>itemlink|direct))?\/(?<propertyName>.+?)(?:\/(?<fileNumber>\d+))?(?:\/)(?<fileName>.+?\..+)", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(2000));
urlRegex = new Regex(@"(?:image\/wiser[0-9]?\/)(?:(?<type>[^\/]+)\/)?(?<encryptedId>.+?)(?:\/(?<fileType>itemlink|direct|name))?\/(?<propertyName>[^\/]+)(?:\/(?<resizeMode>normal|stretch|crop|fill)(?:-(?<anchorPosition>center|top|bottom|left|right|topleft|topright|bottomright|bottomleft))?)?(?:\/(?<preferredWidth>\d+)\/(?<preferredHeight>\d+))?(?:\/(?<fileNumber>\d+))?\/(?<fileName>.+?\..+)", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(2000));
matchResult = urlRegex.Match(path);
if (!matchResult.Success)
{
return;
urlRegex = new Regex(@"(?:file\/wiser[0-9]?\/)(?:(?<type>[^\/]+)\/)?(?<itemId>\d+)(?:\/(?<fileType>itemlink|direct))?\/(?<propertyName>.+?)(?:\/(?<fileNumber>\d+))?(?:\/)(?<fileName>.+?\..+)", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(2000));
matchResult = urlRegex.Match(path);
if (!matchResult.Success)
{
urlRegex = new Regex(@"(?:file\/wiser[0-9]?\/)(?:(?<type>[^\/]+)\/)?(?<encryptedId>.+?)(?:\/(?<fileType>itemlink|direct))?\/(?<propertyName>.+?)(?:\/(?<fileNumber>\d+))?(?:\/)(?<fileName>.+?\..+)", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(2000));
matchResult = urlRegex.Match(path);
if (!matchResult.Success)
{
return;
}
}

context.Request.Path = "/wiser-file.gcl";
}
else
{
context.Request.Path = "/wiser-image.gcl";
}

context.Request.Path = "/wiser-file.gcl";
}
else
{
Expand Down
Loading