commit d6a94ac90dc544c48df1d195bb93bee5b0de308d
parent 0a17c392f0d9f4d2cd04f1ac8112ea40a2cb50ce
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Fri, 1 Nov 2024 10:50:26 +0100
feat: use file content as filename if no link is present
Diffstat:
1 file changed, 23 insertions(+), 13 deletions(-)
diff --git a/lib/helpers.php b/lib/helpers.php
@@ -180,19 +180,29 @@ function generate_filename(string $content) {
}
/* if filename is still unset after this and does have a link,
- * (so the fetching failed), we can use the link itself as title.
- * if the content does NOT contain a link, we use the sha256sum of the
- * content as fallback name */
- if (empty($filename)) {
- if (!empty($file_link)) {
- $filename = preg_replace(
- '/^https{0,1}:\/\/(.*)$/',
- '\1',
- $file_link
- );
- } else {
- $filename = hash("sha256", $content, false);
- }
+ * (so the fetching failed), we can use the link itself as title. */
+ if (empty($filename) && !empty($file_link)) {
+ $filename = preg_replace(
+ '/^https{0,1}:\/\/(.*)$/',
+ '\1',
+ $file_link
+ );
+ $filename = trim($filename); // trim white space
+ }
+
+ /* if filename is still unset (there was no link), try to use the first line
+ * of the file as title (with a maximum character limit) */
+ if (empty($filename)) {
+ $filename = substr(
+ trim(explode(PHP_EOL, $content)[0]),
+ 0,
+ 100 // at max, 100 characters long
+ );
+ }
+
+ /* as a last resort, we use the sha256sum of the content as fallback */
+ if (empty($filename)) {
+ $filename = hash("sha256", $content, false);
}
/* Ensure the new filename only contains valid characters */