summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Attachment
diff options
context:
space:
mode:
authorByron Jones <glob@mozilla.com>2015-07-21 06:58:36 +0200
committerByron Jones <glob@mozilla.com>2015-07-21 06:58:36 +0200
commitec9b5fd81dc74c7f69d221fa529be1c2543a12a0 (patch)
treeb795db3ec24e4426786c9d325af4bd09d53adddc /Bugzilla/Attachment
parentdc8e2b4c3e3a9a14c65f25ba59b4415cdf00c7e2 (diff)
downloadbugzilla-ec9b5fd81dc74c7f69d221fa529be1c2543a12a0.tar.gz
bugzilla-ec9b5fd81dc74c7f69d221fa529be1c2543a12a0.tar.xz
Bug 1180572 - create attachment_storage parameter
Diffstat (limited to 'Bugzilla/Attachment')
-rw-r--r--Bugzilla/Attachment/Database.pm59
-rw-r--r--Bugzilla/Attachment/FileSystem.pm61
2 files changed, 120 insertions, 0 deletions
diff --git a/Bugzilla/Attachment/Database.pm b/Bugzilla/Attachment/Database.pm
new file mode 100644
index 000000000..24617cacc
--- /dev/null
+++ b/Bugzilla/Attachment/Database.pm
@@ -0,0 +1,59 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# This Source Code Form is "Incompatible With Secondary Licenses", as
+# defined by the Mozilla Public License, v. 2.0.
+
+package Bugzilla::Attachment::Database;
+use strict;
+use warnings;
+
+use Bugzilla::Util qw(trick_taint);
+
+sub new {
+ return bless({}, shift);
+}
+
+sub store {
+ my ($self, $attach_id, $data) = @_;
+ my $dbh = Bugzilla->dbh;
+ my $sth = $dbh->prepare("INSERT INTO attach_data (id, thedata) VALUES ($attach_id, ?)");
+ trick_taint($data);
+ $sth->bind_param(1, $data, $dbh->BLOB_TYPE);
+ $sth->execute();
+}
+
+sub retrieve {
+ my ($self, $attach_id) = @_;
+ my $dbh = Bugzilla->dbh;
+ my ($data) = $dbh->selectrow_array(
+ "SELECT thedata FROM attach_data WHERE id = ?",
+ undef,
+ $attach_id
+ );
+ return $data;
+}
+
+sub remove {
+ my ($self, $attach_id) = @_;
+ my $dbh = Bugzilla->dbh;
+ $dbh->do(
+ "DELETE FROM attach_data WHERE id = ?",
+ undef,
+ $attach_id
+ );
+}
+
+sub exists {
+ my ($self, $attach_id) = @_;
+ my $dbh = Bugzilla->dbh;
+ my ($exists) = $dbh->selectrow_array(
+ "SELECT 1 FROM attach_data WHERE id = ?",
+ undef,
+ $attach_id
+ );
+ return !!$exists;
+}
+
+1;
diff --git a/Bugzilla/Attachment/FileSystem.pm b/Bugzilla/Attachment/FileSystem.pm
new file mode 100644
index 000000000..675184c49
--- /dev/null
+++ b/Bugzilla/Attachment/FileSystem.pm
@@ -0,0 +1,61 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# This Source Code Form is "Incompatible With Secondary Licenses", as
+# defined by the Mozilla Public License, v. 2.0.
+
+package Bugzilla::Attachment::FileSystem;
+use strict;
+use warnings;
+
+use Bugzilla::Constants qw(bz_locations);
+
+sub new {
+ return bless({}, shift);
+}
+
+sub store {
+ my ($self, $attach_id, $data) = @_;
+ my $path = _local_path($attach_id);
+ mkdir($path, 0770) unless -d $path;
+ open(my $fh, '>', _local_file($attach_id));
+ binmode($fh);
+ print $fh $data;
+ close($fh);
+}
+
+sub retrieve {
+ my ($self, $attach_id) = @_;
+ if (open(my $fh, '<', _local_file($attach_id))) {
+ local $/;
+ binmode($fh);
+ my $data = <$fh>;
+ close($fh);
+ return $data;
+ }
+ return undef;
+}
+
+sub remove {
+ my ($self, $attach_id) = @_;
+ unlink(_local_file($attach_id));
+}
+
+sub exists {
+ my ($self, $attach_id) = @_;
+ return -e _local_file($attach_id);
+}
+
+sub _local_path {
+ my ($attach_id) = @_;
+ my $hash = sprintf('group.%03d', $attach_id % 1000);
+ return bz_locations()->{attachdir} . '/' . $hash;
+}
+
+sub _local_file {
+ my ($attach_id) = @_;
+ return _local_path($attach_id) . '/attachment.' . $attach_id;
+}
+
+1;