Initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
vendor_perl/**
|
||||||
|
config
|
||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 Sergei Fedosoff <eleksir@gmail.com>
|
||||||
|
|
||||||
|
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.
|
||||||
37
README.md
Normal file
37
README.md
Normal file
@@ -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
|
||||||
5
bootstrap.sh
Normal file
5
bootstrap.sh
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
cpanm -l vendor_perl --force --installdeps .
|
||||||
|
|
||||||
|
# vim: set ft=sh noet ai ts=4 sw=4 sts=4:
|
||||||
1
config_sample
Normal file
1
config_sample
Normal file
@@ -0,0 +1 @@
|
|||||||
|
savedir = /data/imgsave
|
||||||
6
cpanfile
Normal file
6
cpanfile
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
requires 'local::lib';
|
||||||
|
requires 'IO::Socket::SSL';
|
||||||
|
requires 'Plack::Util';
|
||||||
|
requires 'URI::URL';
|
||||||
|
requires 'Coro::LWP';
|
||||||
|
requires 'LWP::Simple';
|
||||||
66
image-dl.psgi
Normal file
66
image-dl.psgi
Normal file
@@ -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:
|
||||||
12
image-dl.yaml
Normal file
12
image-dl.yaml
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user