diff --git a/.gitignore b/.gitignore index d0647d1..e034ab5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ config.cfg .s3cfg +*.pyc \ No newline at end of file diff --git a/README.md b/README.md index 09d649d..d403e4e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # GtkGrab - Screenshot Uploader -Version 0.2.0 Created by Evan Coury +Version 0.3.0 Created by Evan Coury ## Update Notice @@ -39,8 +39,15 @@ Gnome and KDE as well as on Mac OSX. handler.php and chmod it to 777. * Copy the appropriate config.cfg-sample-{os} to config.cfg. Set the username and password to match handler.php and posturl to be the URL to handler.php. + + Create a new top level section in `config.cfg` and pass the section name + into `screenshot` as the first parameter to use different configuration + settings + + * Follow any platform-specific instructions below. + ### Amazon S3 Upload Support * Alternatively, GtkGrab supports using `s3cmd` (available in ruby-gems) to upload screenshots to Amazon S3. @@ -78,7 +85,8 @@ choosing via System -> Preferences -> Keyboard Shortcuts. ### Mac Installation -* Install Growl and growlnotify from http://growl.info/ +* Install Terminal Notifier from https://github.com/julienXX/terminal-notifier/releases +* Copy config.cfg-sample-mac to config.cfg and update as appropriate * Use automator to run **`GtkGrab/screenshot`** via the keyboard shortcut of your choice. Instructions for this can be found [here](http://www.macosxautomation.com/services/learn/tut01/index.html), diff --git a/config.cfg-sample-mac b/config.cfg-sample-mac index cbce99c..4af672b 100644 --- a/config.cfg-sample-mac +++ b/config.cfg-sample-mac @@ -3,5 +3,8 @@ username=myuser password=mypass posturl=http://yourhost.tld/gtkgrab/handler.php command=screencapture -i %s -notifyCommand=/usr/local/bin/growlnotify -m "Copied url to clipboard: %s" -n "GtkGrab" +; If you want to capture the current window without using the mouse, use +; https://github.com/vorgos/QuickGrab and this command: +; command=/usr/local/bin/quickgrab -file %s +notifyCommand=/usr/local/bin/terminal-notifier.app/Contents/MacOS/terminal-notifier -message "Copied URL to clipboard: %s" -title "GtkGrab" capPath=/tmp/ss.png diff --git a/delete-old-files.sh b/delete-old-files.sh new file mode 100644 index 0000000..5a49fa0 --- /dev/null +++ b/delete-old-files.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash + +# Delete PNG files older than 15 months +# Usage: ./delete_old_pngs.sh [directory] [--dry-run] [--verbose] + +DIRECTORY="" +DRY_RUN=false +VERBOSE=false +MONTHS=20 +DAYS=$((MONTHS * 30)) # Calculate days from months (approximate) + +# Parse arguments +while [[ $# -gt 0 ]]; do + case $1 in + --dry-run) + DRY_RUN=true + shift + ;; + --verbose|-v) + VERBOSE=true + shift + ;; + -*) + echo "Unknown option $1" + exit 1 + ;; + *) + DIRECTORY="$1" + shift + ;; + esac +done + +# Default to current directory if not specified +if [[ -z "$DIRECTORY" ]]; then + DIRECTORY="." +fi + +# Check if directory exists +if [[ ! -d "$DIRECTORY" ]]; then + echo "Error: Directory '$DIRECTORY' does not exist" + exit 1 +fi + +echo "Looking for PNG files older than $MONTHS months in: $DIRECTORY" + +if [[ "$DRY_RUN" == true ]]; then + echo "DRY RUN - files that would be deleted:" + find "$DIRECTORY" -name "*.png" -mtime +$DAYS -print +else + # Count files before deletion + count=$(find "$DIRECTORY" -name "*.png" -mtime +$DAYS | wc -l) + + if [[ $count -eq 0 ]]; then + echo "No PNG files older than $MONTHS months found" + exit 0 + fi + + echo "Found $count PNG file(s) older than $MONTHS months" + + # Delete files + if [[ "$VERBOSE" == true ]]; then + find "$DIRECTORY" -name "*.png" -mtime +$DAYS -print -delete + else + find "$DIRECTORY" -name "*.png" -mtime +$DAYS -delete + fi + + echo "Deleted $count PNG file(s)" +fi diff --git a/gtkgrab-current.scpt b/gtkgrab-current.scpt new file mode 100755 index 0000000..c79d04b Binary files /dev/null and b/gtkgrab-current.scpt differ diff --git a/gtkgrab.scpt b/gtkgrab.scpt new file mode 100644 index 0000000..fc8f0ae Binary files /dev/null and b/gtkgrab.scpt differ diff --git a/handler.php b/handler.php old mode 100755 new mode 100644 index 1e7b4d7..4e139f6 --- a/handler.php +++ b/handler.php @@ -39,13 +39,14 @@ // write the file function generateFilename() { - return substr(md5(microtime()),0,6); + return substr(md5(microtime() . mt_rand()),0,8); } do { - $filename = 'caps/'.generateFilename().'.png'; + $filename = generateFilename().'.png'; } while(file_exists($filename)); file_put_contents($filename, base64_decode($input)); +chmod($filename, 0664); if (isset($_SERVER['FCGI_ROLE'])) { $path = $_SERVER['REQUEST_URI']; @@ -57,4 +58,5 @@ function generateFilename() $path .= '/'; } -echo 'http://' . $_SERVER['HTTP_HOST'] . '/' . $path . $filename; +header("Content-Type: text/plain"); +echo 'https://' . $_SERVER['HTTP_HOST'] . '/' . $path . $filename; diff --git a/screenshot b/screenshot index efb408e..e3fa368 100755 --- a/screenshot +++ b/screenshot @@ -1,4 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/python3 + +# !/usr/bin/env python3 + # This file is part of GtkGrab. # # GtkGrab is free software: you can redistribute it and/or modify @@ -19,28 +22,49 @@ # @copyright Copyright 2010-2013 Pieter Kokx (http://kokx.nl/) # @package Client -import ConfigParser, sys, urllib2, os, base64, hashlib, pyperclip, time +import configparser, sys, urllib.request as urllib2, os, base64, hashlib, pyperclip, time -config = ConfigParser.ConfigParser() -config.readfp(open(os.path.dirname(sys.argv[0]) + '/config.cfg')) +config = configparser.ConfigParser() +config.read_file(open(os.path.dirname(__file__) + '/config.cfg')) -user = config.get('GtkGrab','username') -token = config.get('GtkGrab','password') -postURL = config.get('GtkGrab','posturl') -command = config.get('GtkGrab','command') +configSection = 'GtkGrab' +if len(sys.argv) > 1 and sys.argv[1] != 'gif': + configSection = sys.argv[1] +user = config.get(configSection,'username', raw=True) +token = config.get(configSection,'password', raw=True) +postURL = config.get(configSection,'posturl', raw=True) +notifyCommand = config.get(configSection,'notifyCommand', raw=True) +path = config.get(configSection,'capPath', raw=True) + +# determine command to run +command = config.get(configSection,'command', raw=True) if len(sys.argv) > 1 and sys.argv[1] == 'gif': - command = os.path.dirname(__file__) + '/' + config.get('GtkGrab','gifCommand') + command = os.path.dirname(__file__) + '/' + config.get(configSection,'gifCommand', raw=True) +if len(sys.argv) > 2 and sys.argv[2] == 'gif': + command = os.path.dirname(__file__) + '/' + config.get(configSection,'gifCommand', raw=True) + -notifyCommand = config.get('GtkGrab','notifyCommand') +# Delete cap file if it currently exists +if os.path.exists(path) == True: + os.remove(path) -path = config.get('GtkGrab','capPath') +# Perform capture +returnval = os.system(command.replace("%s", path)) + +if (returnval != 0 and returnval != 256) or os.path.exists(path) == False: + # no cap file created + # os.system(notifyCommand.replace('%s', f"{returnval} {path} Cancelled!!!")) + sys.exit(1) + + +# if convertCommand: +# os.system(convertCommand.replace("%s", path)) -os.system(command.replace("%s", path)) if postURL == 's3': - bucket = config.get('GtkGrab', 's3bucket') - prefix = config.get('GtkGrab', 's3prefix') + bucket = config.get(configSection, 's3bucket', raw=True) + prefix = config.get(configSection, 's3prefix', raw=True) filename = hashlib.sha1() filename.update(str(time.time())) filename = filename.hexdigest() @@ -52,19 +76,25 @@ if postURL == 's3': os.system(uploadCommand) url = "http://"+bucket+".s3.amazonaws.com/caps/"+filename+".png" else: + os.system(notifyCommand.replace('%s', f"Uploading")) # encode the screenshot with base64 - data = base64.b64encode(open(path, 'r').read()) + data = base64.b64encode(open(path, 'rb').read()) # hash the data with the user's secret token and send that as header hash = hashlib.sha1() hash.update(data) - hash.update(token) + hash.update(token.encode('UTF-8')) headers = {'X-Username': user, 'X-Signature': hash.hexdigest(), 'Content-Type': 'text/xml'} # upload the url and give the screenshot url to the user + print(postURL) url = urllib2.urlopen(urllib2.Request(postURL, data, headers)).read() + url = url.decode(encoding='utf-8', errors='strict') # remove the file -os.remove(path) +if os.path.exists(path) == True: + os.remove(path) pyperclip.copy(url) -os.system(notifyCommand.replace('%s', url)) +os.system(notifyCommand.replace('%s', 'Copied URL to clipboard: ' + url)) + +os.system(f"open {url}")