From b96acd7c663e0caa1783dcf31091adfe23e9f94d Mon Sep 17 00:00:00 2001 From: cage Date: Fri, 7 Jul 2023 15:34:01 +0200 Subject: [PATCH] - [TUI] allowed upload of strings via titan, not only files. --- src/constants.lisp | 2 ++ src/gemini/titan.lisp | 4 ++-- src/open-message-link-window.lisp | 36 ++++++++++++++++++------------- src/package.lisp | 1 + 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/constants.lisp b/src/constants.lisp index 8c3c22c..9bf8070 100644 --- a/src/constants.lisp +++ b/src/constants.lisp @@ -120,6 +120,8 @@ General Public License for more details." (define-constant +mime-type-html+ "text/html" :test #'string=) +(define-constant +mime-type-text+ "text/plain" :test #'string=) + (define-constant +db-file+ "db.sqlite3" :test #'string= :documentation "the filename of the database") diff --git a/src/gemini/titan.lisp b/src/gemini/titan.lisp index 9dfeec0..70138a7 100644 --- a/src/gemini/titan.lisp +++ b/src/gemini/titan.lisp @@ -57,13 +57,13 @@ (defgeneric titan-request (host path mime-type size token data &key port fragment client-certificate certificate-key)) -(defmethod titan-request (host path mime-type (size integer) token (data sequence) +(defmethod titan-request (host path mime-type (size integer) token (data string) &key (port +gemini-default-port+) (fragment nil) (client-certificate nil) (certificate-key nil)) - (flex:with-input-from-sequence (stream data) + (flex:with-input-from-sequence (stream (text-utils:string->octets data)) (titan-request host path mime-type diff --git a/src/open-message-link-window.lisp b/src/open-message-link-window.lisp index c736ee8..8b9bd31 100644 --- a/src/open-message-link-window.lisp +++ b/src/open-message-link-window.lisp @@ -91,30 +91,36 @@ (db:gemlog-mark-as-seen url) (gemini-viewer:ensure-just-one-stream-rendering) (if (gemini-client:absolute-titan-url-p url) - (let ((upload-file-path nil)) + (let ((upload-file-or-string nil)) (labels ((on-token-input-complete (token) (when (string-not-empty-p token) (db-utils:with-ready-database (:connect nil) (db:save-titan-token url token) - (let ((size (fs:file-size upload-file-path)) - (mime (os-utils:file->mime-type upload-file-path))) + (let* ((pathname (fs:namestring->pathname upload-file-or-string)) + (file-exists-p (fs:file-exists-p pathname)) + (size (if file-exists-p + (fs:file-size pathname) + (length upload-file-or-string))) + (mime (if file-exists-p + (os-utils:file->mime-type pathname) + constants:+mime-type-text+)) + (upload-data (if file-exists-p + pathname + upload-file-or-string))) (gemini-viewer::post-titan-url url - upload-file-path + upload-data size mime token))))) - (on-file-input-complete (file-path) + (on-input-complete (data) (db-utils:with-ready-database (:connect nil) - (if (fs:file-exists-p file-path) - (let ((path-file (fs:namestring->pathname file-path)) - (cached-token (db:saved-titan-token url))) - (setf upload-file-path path-file) - (ui:ask-string-input #'on-token-input-complete - :initial-value cached-token - :prompt - (_ "type access token: "))) - (error (_ "no such file: ~a") file-path))))) - (ui:ask-string-input #'on-file-input-complete + (let ((cached-token (db:saved-titan-token url))) + (setf upload-file-or-string data) + (ui:ask-string-input #'on-token-input-complete + :initial-value cached-token + :prompt + (_ "type access token: ")))))) + (ui:ask-string-input #'on-input-complete :prompt (_ "Upload: ") :complete-fn #'complete:directory-complete))) (gemini-viewer:load-gemini-url url diff --git a/src/package.lisp b/src/package.lisp index a6c6966..28cacf9 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -54,6 +54,7 @@ :+mime-type-jpg+ :+mime-type-png+ :+mime-type-html+ + :+mime-type-text+ :+db-file+ :+fps+ :+command-window-height+