Skip to content

Commit

Permalink
#3215 note: urlencode URL string parts of images and attachments for …
Browse files Browse the repository at this point in the history
…using them in notes and allow management dialogs to recognize them

Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
  • Loading branch information
pbek committed Jan 29, 2025
1 parent f661077 commit 51062f4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# QOwnNotes Changelog

## 25.1.7
- When inserting an image or attachment file via their insert dialogs or via
drag and drop the filenames will now be urlencoded to prevent problems with
special characters in the filenames (for [#3215](https://github.com/pbek/QOwnNotes/issues/3215))
- The management dialogs for images and attachments can now also recognize
urlencoded filenames to show the files correctly

## 25.1.6
- The available AI models for Groq have been updated
(for [#3216](https://github.com/pbek/QOwnNotes/issues/3216), thank you, @Weej1)
Expand Down
17 changes: 15 additions & 2 deletions src/entities/note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ QStringList Note::getMediaFileList() const {
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
const QString fileName = match.captured(1);
fileList << fileName;
// We want to decode the file name parsed from the note, because it can be encoded
// For example the image dialog will automatically encode the file name
fileList << QUrl::fromPercentEncoding(fileName.toUtf8());
}

return fileList;
Expand Down Expand Up @@ -531,7 +533,10 @@ QStringList Note::getAttachmentsFileList() const {
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
const QString fileName = match.captured(1);
fileList << fileName;

// We want to decode the file name parsed from the note, because it can be encoded
// For example the attachment dialog will automatically encode the file name
fileList << QUrl::fromPercentEncoding(fileName.toUtf8());
}

return fileList;
Expand Down Expand Up @@ -3766,6 +3771,10 @@ QString Note::mediaUrlStringForFileName(const QString &fileName) const {
urlString += QStringLiteral("media/") + fileName;
}

// Make sure all spaces and brackets are escaped
// For example this is important in file names like "image (1).png"
urlString = Utils::Misc::encodeFilePath(urlString);

return urlString;
}

Expand All @@ -3785,6 +3794,10 @@ QString Note::attachmentUrlStringForFileName(const QString &fileName) const {
urlString += QStringLiteral("attachments/") + fileName;
}

// Make sure all spaces and brackets are escaped
// For example this is important in file names like "text (1).txt"
urlString = Utils::Misc::encodeFilePath(urlString);

return urlString;
}

Expand Down
19 changes: 19 additions & 0 deletions src/utils/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2656,3 +2656,22 @@ int Utils::Misc::getPreviewRefreshDebounceTime() {
int Utils::Misc::getMaximumNoteFileSize() {
return SettingsService().value(QStringLiteral("maxNoteFileSize"), 1048576).toInt();
}

/**
* Percent encode each individual segment of a file path
* @param filePath
* @return
*/
QString Utils::Misc::encodeFilePath(const QString& filePath) {
// Split the path into segments to preserve the directory separators
QStringList segments = filePath.split('/');

// URL encode each segment individually
for (int i = 0; i < segments.size(); ++i) {
// Use QUrl::toPercentEncoding to properly encode special characters
segments[i] = QUrl::toPercentEncoding(segments[i], "/");
}

// Join the segments back together with forward slashes
return segments.join('/');
}
1 change: 1 addition & 0 deletions src/utils/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ QString getBaseUrlFromUrlString(const QString &urlString, bool withBasePath = fa
QString createAbsolutePathsInHtml(const QString &html, const QString &url);
int getPreviewRefreshDebounceTime();
int getMaximumNoteFileSize();
QString encodeFilePath(const QString& filePath);
} // namespace Misc
} // namespace Utils

Expand Down

0 comments on commit 51062f4

Please sign in to comment.