forked from mogilefs/MogileFS-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mogupload
executable file
·94 lines (59 loc) · 1.97 KB
/
mogupload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/perl
=head1 NAME
mogupload -- Upload data to a MogileFS installation
=head1 SYNOPSIS
$ mogupload [options]
$ mogupload [options] --file="-" < filename
$ mogupload --trackers=host --domain=foo --class=bar \
--key="/hello.jpg" --file="input.jpg"
$ echo "why hello" | mogupload [opts] --key="world" --file="-"
=head1 OPTIONS
=over
=item --trackers=host1:7001,host2:7001
Use these MogileFS trackers to negotiate with.
=item --domain=<domain>
Set the MogileFS domain to use.
=item --class=<class>
Set the class to use. Will use default class if not specified
=item --key="<key>"
A key to store the file under. Can be an arbitrary string.
=item --file="<filename|->"
A local file to upload. If '-', read file from STDIN instead.
=back
=head1 AUTHOR
Dormando E<lt>L<dormando@rydia.net>E<gt>
=head1 BUGS
mogupload must buffer the upload in memory before transferring it. This makes it difficult to upload very large files. Future versions will lift this limitation.
=head1 LICENSE
Licensed for use and redistribution under the same terms as Perl itself.
=cut
use strict;
use warnings;
use lib './lib';
use MogileFS::Utils;
my $util = MogileFS::Utils->new;
my $usage = "--trackers=host --domain=foo --key='/hello.jpg' --file='./hello.jpg'";
my $c = $util->getopts($usage, qw/class=s key=s file=s/);
my $mogc = $util->client;
my $filename = $c->{file};
my $fh;
my $size = 0;
if ($filename eq '-') {
$fh = *STDIN;
} else {
$size = -s $filename;
die "Could not stat " . $filename unless defined $size;
open($fh, "< $filename") or die "Could not open " . $filename;
}
my $mf = $mogc->new_file($c->{key}, $c->{class}, undef);
if ($mogc->errcode) {
die "Error opening MogileFS file: " . $mogc->errstr;
}
my $buf;
while (my $read = read($fh, $buf, 1024 * 1024)) {
die "error reading file" unless defined $read;
$mf->print($buf);
}
unless ($mf->close) {
die "Error writing file: " . $mogc->errcode . ": " . $mogc->errstr;
}