浏览代码

Added linux script

Sergio Brighenti 6 年之前
父节点
当前提交
662cf9bcd2

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@
 + Fontawesome icon match the single file mime-type.
 + Enable audio player with video.js.
 + Video and audio now starts with volume at 50%.
++ Added linux script to allow uploads from linux screenshot tools.
 + Minor layout fixes.
 
 ## v2.2

+ 36 - 2
app/Controllers/UserController.php

@@ -351,10 +351,9 @@ class UserController extends Controller
 			return $response->withRedirect($request->getHeaderLine('HTTP_REFERER'));
 		}
 
-		$base_url = $this->settings['base_url'];
 		$json = [
 			'DestinationType' => 'ImageUploader, TextUploader, FileUploader',
-			'RequestURL' => "$base_url/upload",
+			'RequestURL' => route('upload'),
 			'FileFormName' => 'upload',
 			'Arguments' => [
 				'file' => '$filename$',
@@ -371,6 +370,41 @@ class UserController extends Controller
 			->withJson($json, 200, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
 	}
 
+	/**
+	 * @param Request $request
+	 * @param Response $response
+	 * @param $args
+	 * @return Response
+	 * @throws NotFoundException
+	 * @throws UnauthorizedException
+	 */
+	public function getUploaderScriptFile(Request $request, Response $response, $args): Response
+	{
+		$user = $this->database->query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $args['id'])->fetch();
+
+		if (!$user) {
+			throw new NotFoundException($request, $response);
+		}
+
+		if ($user->id !== Session::get('user_id') && !Session::get('admin', false)) {
+			throw new UnauthorizedException();
+		}
+
+		if ($user->token === null || $user->token === '') {
+			Session::alert('You don\'t have a personal upload token. (Click the update token button and try again)', 'danger');
+			return $response->withRedirect($request->getHeaderLine('HTTP_REFERER'));
+		}
+
+		return $this->view->render($response->withHeader('Content-Disposition', 'attachment;filename="xbackbone_uploader_' . $user->username . '.sh"'),
+			'scripts/xbackbone_uploader.sh.template',
+			[
+				'username' => $user->username,
+				'upload_url' => route('upload'),
+				'token' => $user->token,
+			]
+		);
+	}
+
 	/**
 	 * @return string
 	 */

+ 1 - 0
app/routes.php

@@ -19,6 +19,7 @@ $app->group('', function () {
 	$this->post('/profile/{id}', \App\Controllers\UserController::class . ':profileEdit')->setName('profile.update');
 	$this->post('/user/{id}/refreshToken', \App\Controllers\UserController::class . ':refreshToken')->setName('refreshToken');
 	$this->get('/user/{id}/config/sharex', \App\Controllers\UserController::class . ':getShareXconfigFile')->setName('config.sharex');
+	$this->get('/user/{id}/config/script', \App\Controllers\UserController::class . ':getUploaderScriptFile')->setName('config.script');
 
 	$this->post('/upload/{id}/publish', \App\Controllers\UploadController::class . ':togglePublish')->setName('upload.publish');
 	$this->post('/upload/{id}/unpublish', \App\Controllers\UploadController::class . ':togglePublish')->setName('upload.unpublish');

+ 65 - 0
resources/templates/scripts/xbackbone_uploader.sh.template

@@ -0,0 +1,65 @@
+#!/bin/bash
+
+am_i_root() {
+    if [ "$EUID" -ne 0 ]; then
+        echo "Please run as root or with sudo";
+        exit;
+    fi
+}
+
+create_desktop_entry () {
+cat << "EOF" > "/usr/share/applications/xbackbone-uploader-usr_{{ username }}.desktop"
+[Desktop Entry]
+Encoding=UTF-8
+Exec=&EXEC& %U
+Type=Application
+Terminal=false;
+Comment=Uploader script for XBackBone ({{ username }})
+Name=XBackBone Uploader ({{ username }})
+GenericName=File Uploader
+StartupNotify=false
+Categories=Graphics;
+MimeType=image/bmp;image/jpeg;image/gif;image/png;image/tiff;image/x-bmp;image/x-ico;image/x-png;image/x-pcx;image/x-tga;image/xpm;image/svg+xml;
+NoDisplay=false
+EOF
+
+	sed -i "s:&EXEC&:${1}:g" "/usr/share/applications/xbackbone-uploader-usr_{{ username }}.desktop"
+	echo "Desktop entry created!";
+}
+
+upload () {
+	RESPONSE="$(curl -F "token={{ token }}" -F "upload=@${1}" {{ upload_url }})";
+	
+	if [[ "$(echo "${RESPONSE}" | jq -r '.message')" == "OK." ]]; then
+		URL="$(echo "${RESPONSE}" | jq -r '.url')";
+		echo "${URL}" | xclip -selection c;
+		notify-send "Upload completed!" "${URL}";
+	else
+		notify-send "Error!" "$(echo "${RESPONSE}" | jq -r '.message')";
+	fi
+}
+
+check () {
+	if [ ! -x "$(command -v xclip)" ]; then
+		echo "xclip command not found."
+        exit;
+	fi	
+
+	if [ ! -x "$(command -v curl)" ]; then
+		echo "curl command not found."
+        exit;
+	fi
+
+	if [ ! -x "$(command -v notify-send)" ]; then
+		echo "notify-send command not found."
+        exit;
+	fi
+}
+
+check
+if [[ "${1}" == "-desktop-entry" ]]; then
+	am_i_root
+    create_desktop_entry "$(realpath "${0}")";
+else
+	upload "${1}";
+fi

+ 4 - 1
resources/templates/user/edit.twig

@@ -51,7 +51,10 @@
                             <div class="form-group row">
                                 <label class="col-sm-2 col-form-label">{{ lang('client_config') }}</label>
                                 <div class="col-sm-10">
-                                    <a href="{{ route('config.sharex', {'id': user.id}) }}" class="btn btn-lg btn-outline-dark"><i class="fas fa-fw fa-download"></i> ShareX Config</a>
+                                    <div class="btn-group">
+                                        <a href="{{ route('config.sharex', {'id': user.id}) }}" class="btn btn-lg btn-outline-dark"><i class="fas fa-fw fa-download"></i> ShareX Config</a>
+                                        <a href="{{ route('config.script', {'id': user.id}) }}" class="btn btn-lg btn-outline-danger"><i class="fas fa-fw fa-download"></i> Linux Script</a>
+                                    </div>
                                 </div>
                             </div>
                             {% if not profile %}