Mojo 作为文件共享服务器
发表于 : 2023年10月12日 18:14
日常小工具,用来把当前文件夹的特定格式文件开放到服务器,便于手机访问下载。
我会这么做是因为,今天用QQ下载电脑传的文件,竟然反复失败……;用微信传文件,要去文件夹里翻,实在受不了。
代码: 全选
# code by 523066680 funicode.net
use Mojolicious::Lite;
use File::Basename;
use URI::Escape;
STDOUT->autoflush(1);
my $app = app;
my $static = $app->static;
my $pwd = $ENV{PWD};
my $port = 3000;
my $localhost = get_localhost_ip();
push @{$static->paths}, ( $pwd );
push @{$static->paths}, ( "E:/Temp/Share" );
my @files = glob "*.mp4";
for my $e ( @files )
{
my $url = sprintf "http://%s:%s/%s", $localhost, $port, uri_escape(basename($e));
printf "%s\n", $url;
}
get '/' => sub {
my $self = shift;
#... do something for a homepage here ...
};
$app->start('daemon', '-l', "http://*:${port}");
sub get_localhost_ip
{
my $result = `ipconfig /all`;
if ( $result =~ /WLAN:.*?IPv4 .*?:\s+([\d\.]+)/msi )
{
return $1;
}
}
其中 get_localhost_ip 函数用于从 ipconfig /all 中获取本机的局域网IP