summaryrefslogtreecommitdiff
path: root/src/app.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2021-02-26 10:24:09 +0200
committerJaakko Keränen <jaakko.keranen@iki.fi>2021-02-26 10:24:09 +0200
commit91a6225d8508db01574d7da2c013cb30d6a87ec8 (patch)
treee3bd2c2f24a22c694c1c23aefd5fc531ae108723 /src/app.c
parent4708a6580e9af65cd15769e87487fdf4456f1e00 (diff)
DocumentWidget: Inline downloads
Diffstat (limited to 'src/app.c')
-rw-r--r--src/app.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/app.c b/src/app.c
index c04347b0..ed2c71b4 100644
--- a/src/app.c
+++ b/src/app.c
@@ -581,6 +581,61 @@ const iString *downloadDir_App(void) {
581 return collect_String(cleaned_Path(&app_.prefs.downloadDir)); 581 return collect_String(cleaned_Path(&app_.prefs.downloadDir));
582} 582}
583 583
584const iString *downloadPathForUrl_App(const iString *url, const iString *mime) {
585 /* Figure out a file name from the URL. */
586 iUrl parts;
587 init_Url(&parts, url);
588 while (startsWith_Rangecc(parts.path, "/")) {
589 parts.path.start++;
590 }
591 while (endsWith_Rangecc(parts.path, "/")) {
592 parts.path.end--;
593 }
594 iString *name = collectNewCStr_String("pagecontent");
595 if (isEmpty_Range(&parts.path)) {
596 if (!isEmpty_Range(&parts.host)) {
597 setRange_String(name, parts.host);
598 replace_Block(&name->chars, '.', '_');
599 }
600 }
601 else {
602 iRangecc fn = { parts.path.start + lastIndexOfCStr_Rangecc(parts.path, "/") + 1,
603 parts.path.end };
604 if (!isEmpty_Range(&fn)) {
605 setRange_String(name, fn);
606 }
607 }
608 if (startsWith_String(name, "~")) {
609 /* This would be interpreted as a reference to a home directory. */
610 remove_Block(&name->chars, 0, 1);
611 }
612 iString *savePath = concat_Path(downloadDir_App(), name);
613 if (lastIndexOfCStr_String(savePath, ".") == iInvalidPos) {
614 /* No extension specified in URL. */
615 if (startsWith_String(mime, "text/gemini")) {
616 appendCStr_String(savePath, ".gmi");
617 }
618 else if (startsWith_String(mime, "text/")) {
619 appendCStr_String(savePath, ".txt");
620 }
621 else if (startsWith_String(mime, "image/")) {
622 appendCStr_String(savePath, cstr_String(mime) + 6);
623 }
624 }
625 if (fileExists_FileInfo(savePath)) {
626 /* Make it unique. */
627 iDate now;
628 initCurrent_Date(&now);
629 size_t insPos = lastIndexOfCStr_String(savePath, ".");
630 if (insPos == iInvalidPos) {
631 insPos = size_String(savePath);
632 }
633 const iString *date = collect_String(format_Date(&now, "_%Y-%m-%d_%H%M%S"));
634 insertData_Block(&savePath->chars, insPos, cstr_String(date), size_String(date));
635 }
636 return collect_String(savePath);
637}
638
584const iString *debugInfo_App(void) { 639const iString *debugInfo_App(void) {
585 extern char **environ; /* The environment variables. */ 640 extern char **environ; /* The environment variables. */
586 iApp *d = &app_; 641 iApp *d = &app_;