Browse Source

Small script improvement

Sergio Brighenti 6 years ago
parent
commit
cae02d816f

+ 1 - 1
app/Controllers/UserController.php

@@ -396,7 +396,7 @@ class UserController extends Controller
 		}
 		}
 
 
 		return $this->view->render($response->withHeader('Content-Disposition', 'attachment;filename="xbackbone_uploader_' . $user->username . '.sh"'),
 		return $this->view->render($response->withHeader('Content-Disposition', 'attachment;filename="xbackbone_uploader_' . $user->username . '.sh"'),
-			'scripts/xbackbone_uploader.sh.template',
+			'scripts/xbackbone_uploader.sh.twig',
 			[
 			[
 				'username' => $user->username,
 				'username' => $user->username,
 				'upload_url' => route('upload'),
 				'upload_url' => route('upload'),

+ 26 - 21
resources/templates/scripts/xbackbone_uploader.sh.template → resources/templates/scripts/xbackbone_uploader.sh.twig

@@ -7,7 +7,7 @@ am_i_root() {
     fi
     fi
 }
 }
 
 
-create_desktop_entry () {
+create_desktop_entry() {
 cat << "EOF" > "/usr/share/applications/xbackbone-uploader-usr_{{ username }}.desktop"
 cat << "EOF" > "/usr/share/applications/xbackbone-uploader-usr_{{ username }}.desktop"
 [Desktop Entry]
 [Desktop Entry]
 Encoding=UTF-8
 Encoding=UTF-8
@@ -23,43 +23,48 @@ MimeType=image/bmp;image/jpeg;image/gif;image/png;image/tiff;image/x-bmp;image/x
 NoDisplay=false
 NoDisplay=false
 EOF
 EOF
 
 
-	sed -i "s:&EXEC&:${1}:g" "/usr/share/applications/xbackbone-uploader-usr_{{ username }}.desktop"
-	echo "Desktop entry created!";
+    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
+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 () {
+check() {
 	if [ ! -x "$(command -v xclip)" ]; then
 	if [ ! -x "$(command -v xclip)" ]; then
-		echo "xclip command not found."
+        echo "xclip command not found."
         exit;
         exit;
 	fi	
 	fi	
 
 
 	if [ ! -x "$(command -v curl)" ]; then
 	if [ ! -x "$(command -v curl)" ]; then
-		echo "curl command not found."
+        echo "curl command not found."
         exit;
         exit;
 	fi
 	fi
 
 
 	if [ ! -x "$(command -v notify-send)" ]; then
 	if [ ! -x "$(command -v notify-send)" ]; then
-		echo "notify-send command not found."
+        echo "notify-send command not found."
         exit;
         exit;
 	fi
 	fi
 }
 }
 
 
 check
 check
-if [[ "${1}" == "-desktop-entry" ]]; then
-	am_i_root
+if [ "${1}" == "-desktop-entry" ]; then
+    am_i_root
     create_desktop_entry "$(realpath "${0}")";
     create_desktop_entry "$(realpath "${0}")";
-else
-	upload "${1}";
+    exit;
 fi
 fi
+
+if [ -f "${1}" ]; then
+    upload "${1}";
+else
+    notify-send "Error!" "File specified not exists.";
+fi