Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix for Bug # 1038. Media files are now stored in directories with me…

…dia IDs

broken into two-character strings. Version directories now have "v." prepended
to them to disambiguate them from ID directory parts.

So, for example, if you had a media document "foo.txt" with the ID 1024 with
versions 0-3, where previously you had:

  /1024/0/foo.txt
  /1024/1/foo.txt
  /1024/2/foo.txt
  /1024/3/foo.txt

Now you have:

  /10/24/v.0/foo.txt
  /10/24/v.1/foo.txt
  /10/24/v.2/foo.txt
  /10/24/v.3/foo.txt

And if you had a media document "bar.txt" with the ID 10242, you will now have:

  /10/24/2/v.0/bar.txt
  /10/24/2/v.1/bar.txt
  /10/24/2/v.2/bar.txt

So now the largest number of subdirectories possible in any directory is the
number of versions of a media document plus 100 (0-99). Let us hope that no
one ever creates 30,998 versions of a single media document--at least not
before they upgrade their file system!
  • Loading branch information...
commit 37c3ac85006503bde4240f642a315ff4c3fb425b 1 parent a6403d5
@theory theory authored
View
62 inst/upgrade/1.8.7/move_media.pl
@@ -0,0 +1,62 @@
+#!/usr/bin/perl -w
+
+use strict;
+use FindBin;
+use File::Spec::Functions qw(catdir updir);
+use lib catdir $FindBin::Bin, updir, 'lib';
+use Bric::Config qw(MASON_COMP_ROOT);
+use Bric::Util::Trans::FS;
+use File::Copy qw(mv);
+
+my $fs = Bric::Util::Trans::FS->new;
+my $orig_dir = $fs->cat_dir(MASON_COMP_ROOT->[0][1], qw(data media));
+my $temp_dir = $fs->cat_dir(MASON_COMP_ROOT->[0][1], qw(data temp));
+
+# Check the media directory to see if upgrade script has already run.
+opendir my $check_dir, $orig_dir or die "Cannot open $orig_dir: $!\n";
+while (my $check_file = readdir $check_dir) {
+ exit if $check_file =~ /^\d\d$/; # New directory convention.
+ last if $check_file =~ /^\d{4,}$/; # Old directory convention.
+}
+closedir $check_dir;
+
+# Move the old media directory out of the way.
+mv $orig_dir, $temp_dir;
+
+# Create a new media directory.
+$fs->mk_path($orig_dir);
+
+# Now rename all of the directories.
+opendir my $dir, $temp_dir or die "Cannot open $temp_dir: $!\n";
+while (my $media_id = readdir $dir) {
+ next unless $media_id =~ /^\d+$/;
+ my $media_dir = $fs->cat_dir($temp_dir, $media_id);
+ next unless -d $media_dir;
+
+ # Create the new directory.
+ my @dirs = $media_id =~ /(\d\d?)/g;
+ my $new_dir = $fs->cat_dir($orig_dir, @dirs);
+ $fs->mk_path($new_dir);
+
+ # Now copy over all the versions.
+ opendir my $old_dir, $media_dir or die "Cannot open $media_dir: $!\n";
+ while (my $version = readdir $old_dir) {
+ next unless $version =~ /^\d+$/;
+ my $old_vdir = $fs->cat_dir($media_dir, $version);
+ next unless -d $old_vdir;
+
+ opendir my $vdir, $old_vdir or die "Cannot open $old_vdir: $!\n";
+ while (my $file = readdir $vdir) {
+ next if $file eq '.' or $file eq '..';
+ my $old_file = $fs->cat_file($old_vdir, $file);
+ my $file_dir = $fs->cat_dir($new_dir, "v.$version");
+ $fs->mk_path($file_dir);
+ mv $old_file, $file_dir;
+ }
+ }
+}
+
+$fs->del($temp_dir);
+
+__END__
+
View
34 inst/upgrade/1.8.7/move_media2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl -w
+
+use strict;
+use FindBin;
+use File::Spec::Functions qw(catdir updir);
+use lib catdir $FindBin::Bin, updir, 'lib';
+use bric_upgrade qw(:all);
+use Bric::Util::DBI qw(:all);
+use Bric::Util::Trans::FS;
+
+exit unless fetch_sql q{
+ SELECT 1
+ FROM media_instance
+ WHERE location LIKE '/____/0/%'
+ LIMIT 1
+};
+
+my $fs = Bric::Util::Trans::FS->new;
+my $sel = prepare('SELECT id, location FROM media_instance WHERE location IS NOT NULL');
+my $upd = prepare('UPDATE media_instance SET location = ? WHERE id = ?');
+
+execute($sel);
+bind_columns($sel, \my ($id, $loc));
+
+while (fetch($sel)) {
+ my ($empty, $media_id, $version, $file) = split m{/}, $loc;
+ my @dirs = $media_id =~ /(\d\d?)/g;
+ my $uri = $fs->cat_uri($empty, @dirs, "v.$version", $file);
+ execute($upd, $uri, $id);
+}
+
+
+__END__
+
View
2  inst/upgrade/1.8.7/postgresql_8.1_fixes.pl
@@ -6,8 +6,6 @@
use lib catdir $FindBin::Bin, updir, 'lib';
use bric_upgrade qw(:all);
-
-
do_sql q{
ALTER TABLE attr_grp_meta
ALTER COLUMN id SET DEFAULT NEXTVAL('seq_attr_grp_meta')
View
5 lib/Bric/Biz/Asset/Business/Media.pm
@@ -1442,7 +1442,8 @@ sub upload_file {
my ($id, $v, $old_fn, $loc, $uri) =
$self->_get(qw(id version file_name location uri));
- my $dir = Bric::Util::Trans::FS->cat_dir(MEDIA_FILE_ROOT, $id, $v);
+ my @id_dirs = $id =~ /(\d\d?)/g;
+ my $dir = Bric::Util::Trans::FS->cat_dir(MEDIA_FILE_ROOT, @id_dirs, "v.$v");
Bric::Util::Trans::FS->mk_path($dir);
my $path = Bric::Util::Trans::FS->cat_dir($dir, $name);
@@ -1474,7 +1475,7 @@ sub upload_file {
my $at_obj = $self->_get_element_object;
my $oc_obj = $self->get_primary_oc;
- my $new_loc = Bric::Util::Trans::FS->cat_dir('/', $id, $v, $name);
+ my $new_loc = Bric::Util::Trans::FS->cat_dir('/', @id_dirs, "v.$v", $name);
# Set the location, name, and URI.
if (not defined $old_fn

0 comments on commit 37c3ac8

Please sign in to comment.
Something went wrong with that request. Please try again.