commit 29acad6d7dba5452780b7fcabdf1856e713ddb7f Author: eleksir Date: Mon Dec 14 23:09:31 2020 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ce0d981 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor_perl/** +config diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b010026 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Sergei Fedosoff + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e531ab9 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Image-dl + +## What is this? + +Image downloading tool. + +## What for + +To download images. It can be used for im clients to save images via custom plugins. + +## How it works + +Asynchronously downloads given picture. + +```bash +curl -H 'url: http://server.tld/image.jpg' http://localhost:3088/image_dl +``` + +## How to run it + +As [PSGI][1] application, via [UWSGI][2]. + +Before running this app should be bootstrapped via bootstrap.sh and [cpanm][3]. + +Copy config_sample to config open it and edit, put there valid dir where images will be saved. + +```bash +/usr/bin/uwsgi --yaml /var/www/image-dl/image-dl.yaml \ + --psgi /var/www/image-dl/image-dl.psgi \ + --logto /var/log/image-dl.log \ + --pidfile /var/run/image-dl.pid \ + --perl-local-lib /var/www/image-dl/vendor_perl +``` + +[1]: https://uwsgi-docs.readthedocs.io/en/latest/Perl.html +[2]: https://github.com/unbit/uwsgi +[3]: https://github.com/miyagawa/cpanminus diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100644 index 0000000..5cf8db6 --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +cpanm -l vendor_perl --force --installdeps . + +# vim: set ft=sh noet ai ts=4 sw=4 sts=4: diff --git a/config_sample b/config_sample new file mode 100644 index 0000000..3518c7b --- /dev/null +++ b/config_sample @@ -0,0 +1 @@ +savedir = /data/imgsave diff --git a/cpanfile b/cpanfile new file mode 100644 index 0000000..4afb7cc --- /dev/null +++ b/cpanfile @@ -0,0 +1,6 @@ +requires 'local::lib'; +requires 'IO::Socket::SSL'; +requires 'Plack::Util'; +requires 'URI::URL'; +requires 'Coro::LWP'; +requires 'LWP::Simple'; diff --git a/image-dl.psgi b/image-dl.psgi new file mode 100644 index 0000000..460e6e7 --- /dev/null +++ b/image-dl.psgi @@ -0,0 +1,66 @@ +use 5.018; +use strict; +use warnings; +use utf8; +use open qw(:std :utf8); +use lib qw(./vendor_perl/lib/perl5); +use URI::URL; +use LWP::Simple qw(mirror); +use Coro::LWP; + +open (my $ch, '<', './config') || die "No config.\n"; +my $savedir = readline $ch; +close $ch; +chomp $savedir; +$savedir = (split (/\s*=\s*/, $savedir, 2))[1]; + +my $app = sub { + my $env = shift; + + my $msg = "Your Opinion is very important for us, please stand by.\n"; + my $status = '404'; + my $content = 'text/plain'; + + if ($env->{PATH_INFO} eq "/image_dl") { + my $str = $env->{HTTP_URL}; + + unless ((substr ($str, 0, 7) eq 'http://') || (substr ($str, 0, 8) eq 'https://')) { + $msg = "Incorrect URL\n"; + return [ '400', [ 'Content-Type' => $content, 'Content-Length' => length ($msg) ], [ $msg ], ]; + } + + unless (-d $savedir) { + mkdir ($savedir) or do { + $msg = "Unable to make $savedir: $!\n"; + return [ '500', [ 'Content-Type' => $content, 'Content-Length' => length ($msg) ], [ $msg ], ]; + } + } + + my $fname = $str; + $fname =~ s/[^\w!., -#]/_/xmsg; + + if (lc ($str) =~ /\.(gif|jpe?g|png|webm|mp4)$/xms) { + my $savepath = sprintf '%s/%s', $savedir, $fname; + + unless (-f $savepath) { + my $urlobj = url $str; + mirror ($urlobj->as_string, $savepath); + } + } + + $msg = "Done\n"; + $status = '200'; + } + + + return [ + $status, + [ 'Content-Type' => $content, 'Content-Length' => length ($msg) ], + [ $msg ], + ]; +}; + + +__END__ + +# vim: ft=perl noet ai ts=4 sw=4 sts=4: diff --git a/image-dl.yaml b/image-dl.yaml new file mode 100644 index 0000000..1604315 --- /dev/null +++ b/image-dl.yaml @@ -0,0 +1,12 @@ +uwsgi: + http-socket: 127.0.0.1:3088 + plugins: psgi + logformat: %(var.REMOTE_ADDR) %(user_id) %(var.REQUEST_METHOD) %(var.REQUEST_URI) %(var.SERVER_PROTOCOL) %(status) + processes: 1 + need-app: true + log-reopen: yes + reload-mercy: 5 + buffer-size: 32768 + die-on-term: true + enable-threads: false + perl-exec: /dev/zero