summaryrefslogtreecommitdiff
path: root/magic-renamer.pl
blob: 12a85aaddde87b55c077339e1b6602f06dc73acb (plain)
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
# sometimes you just get  unnamed files
# sometimes you don't, count yourself lucky
#
# but if you do not fear, libmagic is here.
#
#
#
# File recovery, snappy-fix, whatever
#
# perl ./magic-renamer.pl $input_dir $output_dir
#
# magic renamer will take files regardless of extension
# and copy them into a dest folder with their respective 
# real extensions.
#
# ex:
#
# input/1a915afe2  <- this is a recovered file... it's really a jpg
# input/42ae2bce4  <- this is a recovered file... it's really an svg
#
# result
#
# output/1a915afe2.png
# output/42ae2bce4.svg
use File::LibMagic;
use File::Copy;
opendir my $DIR, $ARGV[0] or die "Cannot open directory: $!";

my $magic = File::LibMagic->new;
while( ($filename = readdir($DIR))) {
  my $source = $ARGV[0] . '/' . $filename;
  my $dest = $ARGV[1] . '/' . $filename;

  my $info = $magic->info_from_filename($source);
  if($info->{description} =~ /SVG/) {
    print "copy to $dest.svg\n";
    copy($source, $dest . ".svg");
  }
  elsif($info->{description} =~ /JPEG/) {
    print "copy to $dest.jpg\n";
    copy($source, $dest . ".jpg");
  }
  elsif($info->{description} =~ /GIF/) {
    print "copy to $dest.gif\n";
    copy($source, $dest . ".gif");
  }
  elsif($info->{description} =~ /PNG/) {
    print "copy to $dest.png\n";
    copy($source, $dest . ".png");
  }
  elsif($info->{description} =~ /RIFF/) {
    print "copy to $dest.webp\n";
    copy($source, $dest . ".webp");
  }
  elsif($info->{description} =~ /MP4/) {
    print "copy to $dest.mp4\n";
    copy($source, $dest . ".mp4");
  }
  else {
    print "ignoring $source\n";
  }

}