]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/MID.pm
view: deduplicate common code for loading search results
[public-inbox.git] / lib / PublicInbox / MID.pm
index e5a30a1b0924c04ed721837f47ba49b2f411ddcb..d097011bf4ae21b58075def04f2306cd66a22f54 100644 (file)
@@ -4,7 +4,7 @@ package PublicInbox::MID;
 use strict;
 use warnings;
 use base qw/Exporter/;
-our @EXPORT_OK = qw/mid_clean mid_compressed/;
+our @EXPORT_OK = qw/mid_clean mid_compressed mid2path/;
 use Digest::SHA qw/sha1_hex/;
 use constant MID_MAX => 40; # SHA-1 hex length
 
@@ -24,4 +24,16 @@ sub mid_compressed {
        sha1_hex($mid);
 }
 
+sub mid2path {
+       my ($mid) = @_;
+       my ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
+
+       unless (defined $x38) {
+               # compatibility with old links (or short Message-IDs :)
+               $mid = sha1_hex($mid);
+               ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
+       }
+       "$x2/$x38";
+}
+
 1;