From 36105e86a58d83b6d2e766911124ffccc245fc25 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 27 Jun 2020 17:50:28 +0200 Subject: [PATCH] Add upload_file.sh and update file-upload view accordingly --- assets/files/upload_file.sh | 18 ++++++ src/controllers/FileController.ts | 5 ++ views/file-upload.njk | 94 ++++++++++++++++++------------- 3 files changed, 78 insertions(+), 39 deletions(-) create mode 100755 assets/files/upload_file.sh diff --git a/assets/files/upload_file.sh b/assets/files/upload_file.sh new file mode 100755 index 0000000..4239746 --- /dev/null +++ b/assets/files/upload_file.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +local_file=$1 +token=$(cat $HOME/.ily_token) + +if test -z "$2"; then + curl -X POST -H 'Accept: text/plain' \ + -H "Authorization: $token" \ + -F 'upload=@'$local_file \ + -F 'ttl=2592000' \ + https://ily.li/ +else + curl -X PUT -H 'Accept: text/plain' \ + -H "Authorization: $token" \ + -F 'upload=@'$local_file \ + -F 'ttl=2592000' \ + "https://ily.li/$2" +fi diff --git a/src/controllers/FileController.ts b/src/controllers/FileController.ts index 6f6a9c3..bbf7972 100644 --- a/src/controllers/FileController.ts +++ b/src/controllers/FileController.ts @@ -15,6 +15,7 @@ const SLUG_DICTIONARY = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012 export default class FileController extends Controller { routes(): void { this.get('/files/upload', this.getFileUploader, 'file-upload', REQUIRE_AUTH_MIDDLEWARE); + this.get('/files/upload/script', this.downloadLinuxScript, 'linux-script'); this.post('/files/post', this.postFileFrontend, 'post-file-frontend', REQUIRE_AUTH_MIDDLEWARE, FILE_UPLOAD_FORM_MIDDLEWARE); this.get('/files/:page([0-9]+)?', this.getFileManager, 'file-manager', REQUIRE_AUTH_MIDDLEWARE); this.get('/files/delete/:slug', this.deleteFile, 'delete-file-frontend', REQUIRE_AUTH_MIDDLEWARE); @@ -34,6 +35,10 @@ export default class FileController extends Controller { }); } + protected async downloadLinuxScript(req: Request, res: Response): Promise { + res.download('assets/files/upload_file.sh', 'upload_file.sh'); + } + protected async getFileManager(req: Request, res: Response): Promise { res.render('file-manager', { files: await FileModel.paginateForUser(req, 100, req.models.user!.id!), diff --git a/views/file-upload.njk b/views/file-upload.njk index 52997d7..c0d4cc9 100644 --- a/views/file-upload.njk +++ b/views/file-upload.njk @@ -46,55 +46,72 @@

Setup a desktop utility

There may be a desktop client at some point. For now, if you're an advanced user, you can setup scripts/macros.

-

- To upload the file, you must: -

-
    -
  • Set the "Authorization" HTTP header to an auth token (generate one with the form below)
  • -
  • Make a proper file upload request either with the method "POST" on / (auto-generates a short url) or - "PUT" (choose the target url you want, alphanum) -
  • -
- - - - - - - - - - - - - - - - +
+
+

First alternative: sh script (native on linux)

+

If you have the sh shell on your machine (i.e. you are on linux, git bash on windows...), you can + download this script.

+

You must put a valid auth token (generated in the form at the bottom of this page) in a .ily_token file in your home directory ($HOME/.ily_token).

+

Examples:

+
upload_script.sh path/to/file
+
upload_script.sh path/to/file my_very_important_file.png
+
- - - - - - - -
Field nameDescriptionOptional?Example
uploadThe file fieldNo-
ttlHow much time (in seconds) to keep the fileYes0 (never delete), 30 (delete after 30s)
-

Example with curl:

-
curl -X POST -H 'Accept: application/json' \
+            
+ +
+

Second alternative: implement your own client

+

+ To upload the file, you must: +

+
    +
  • Set the "Authorization" HTTP header to an auth token (generate one with the form below)
  • +
  • Make a proper file upload request either with the method "POST" on / (auto-generates a short + url) or "PUT" (choose the target url you want, alphanum) +
  • +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
Field nameDescriptionOptional?Example
uploadThe file fieldNo-
ttlHow much time (in seconds) to keep the fileYes0 (never delete), 30 (delete after 30s)
+

Example with curl:

+
curl -X POST -H 'Accept: application/json' \
 -H "Authorization: very_secret_and_personal_token" \
 -F 'upload=@path/to/local/file' \
 https://ily.li/
-
curl -X PUT -H 'Accept: application/json' \
+                
curl -X PUT -H 'Accept: application/json' \
 -H "Authorization: very_secret_and_personal_token" \
 -F 'upload=@path/to/local/file' \
 https://ily.li/my_very_important_file.png
-
curl -X POST -H 'Accept: application/json' \
+                
curl -X POST -H 'Accept: application/json' \
 -H "Authorization: very_secret_and_personal_token" \
 -F 'upload=@path/to/local/file' \
 -F 'ttl=30' \
 https://ily.li/
+
@@ -130,8 +147,7 @@ https://ily.li/
{{ token.created_at.toISOString() }} {{ token.used_at.toISOString() }} - Revoke + Revoke {% endfor %}