-
Notifications
You must be signed in to change notification settings - Fork 0
/
OwnCloud.pm
175 lines (138 loc) · 4.64 KB
/
OwnCloud.pm
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#! /usr/bin/env perl
package OwnCloud;
use lib $ENV{'SHUTTER_ROOT'} . '/share/shutter/resources/modules';
use utf8;
use strict;
use POSIX qw/setlocale/;
use File::Basename;
use URI::Escape;
use Locale::gettext;
use Glib qw/TRUE FALSE/;
use Shutter::Upload::Shared;
our @ISA = qw(Shutter::Upload::Shared);
my $d = Locale::gettext->domain("shutter-plugins");
$d->dir( $ENV{'SHUTTER_INTL'} );
my %upload_plugin_info = (
module => "OwnCloud",
url => "https://owncloud.net/",
registration => "",
description => $d->get("Upload screenshots to owncloud storage"),
supports_anonymous_upload => FALSE,
supports_authorized_upload => TRUE,
);
binmode( STDOUT, ":utf8" );
if ( exists $upload_plugin_info{ $ARGV[0] } ) {
print $upload_plugin_info{ $ARGV[0] };
exit;
}
#don't touch this
sub new {
my $class = shift;
#call constructor of super class (host, debug_cparam, shutter_root, gettext_object, main_gtk_window, ua)
my $self = $class->SUPER::new( shift, shift, shift, shift, shift, shift );
bless $self, $class;
return $self;
}
#load some custom modules here (or do other custom stuff)
sub init {
my $self = shift;
use HTTP::DAV;
use XML::XPath;
return TRUE;
}
sub will_not_clobber {
my ( $new_file, @existing ) = @_;
my %existing = map { $_ => 1 } @existing;
my ( $filename, $ext ) = split /\./, $new_file;
my $rv = $new_file;
my $count = 1;
while ( $existing{$rv} ) {
$rv = "$filename-$count" . ( $ext ? ".$ext" : "" );
++$count;
}
return $rv;
}
#handle
sub upload {
my ( $self, $upload_filename, $username, $password ) = @_;
#store as object vars
$self->{_filename} = $upload_filename;
$self->{_username} = $username;
$self->{_password} = $password;
utf8::encode $upload_filename;
utf8::encode $password;
utf8::encode $username;
my $webdav = HTTP::DAV->new;
my $url = $upload_plugin_info{url} . '/remote.php/webdav';
my $dir = 'Screenshots';
my $basename = uri_escape( basename($upload_filename) );
#username/password are provided
if ( $username ne "" && $password ne "" ) {
eval {
$webdav->credentials(
-user => $username,
-pass => $password,
-url => $url,
);
unless ( $webdav->open( -url => $url ) ) {
$self->{_links}{status} = 999;
die $webdav->message;
}
};
if ($@) {
$self->{_links}{'status'} = $@;
return %{ $self->{_links} };
}
if ( $self->{_links}{'status'} == 999 ) {
return %{ $self->{_links} };
}
}
#upload the file
eval {
#########################
#put the upload code here
#########################
if ( !$webdav->cwd($dir) ) {
$webdav->mkcol($dir) and $webdav->cwd($dir)
or die "Cannot open $dir directory: " . $webdav->message . "\n";
}
my $r = $webdav->propfind("")
or die "Cannot PROPFIND $dir: " . $webdav->message . "\n";
# if found anything then try not to clobber
if ( $r->get_resourcelist ) {
$basename = will_not_clobber( $basename,
map { basename( $_->get_uri->path ) }
$r->get_resourcelist->get_resources );
}
$webdav->put(
-local => $upload_filename,
-url => "$url/$dir/$basename"
)
or die "Cannot upload to $url/$dir/$basename: "
. $webdav->message . "\n";
my $ua = $webdav->get_user_agent;
my $resp = $ua->post(
$upload_plugin_info{url}
. "/ocs/v1.php/apps/files_sharing/api/v1/shares",
{ path => "$dir/$basename", shareType => 3 }
);
$resp->code == 200
or die "Cannot publish $basename: " . $resp->code . "; ",
$resp->message . "\n";
my $xp = XML::XPath->new( xml => $resp->content );
my $publish_code = $xp->findvalue('/ocs/meta/statuscode');
my $publish_url = $xp->findvalue('/ocs/data/url');
$publish_code->value() == "100"
or die "Cannot publish $basename: " . "\n";
$self->{_links}{direct_link} = $publish_url->value();
$self->{_links}{'status'} = 200;
};
if ($@) {
$self->{_links}{'status'} = $@;
}
#and return links
return %{ $self->{_links} };
}
#you are free to implement some custom subs here, but please make sure they don't interfere with Shutter's subs
#hence, please follow this naming convention: _<provider>_sub (e.g. _imageshack_convert_x_to_y)
1;