blib/lib/File/UStore.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 161 SCAR 48.8
CC
11
Cov
0%
new
42.0
remove
12.0
add
6.0
CRAP 132.0
1 package File::UStore;
2  
3 use strict;
4 use warnings;
5  
6 # PODNAME: File::UStore
7  
8 # ABSTRACT: UUID based File Storage Module.
9  
10 our $VERSION = '0.15'; # VERSION
11  
12 # Dependencies
13  
14 use 5.006;
15 use UUID;
16 use File::Copy;
17 use File::Path;
18 use File::Spec;
19  
20 require Exporter;
21 use AutoLoader qw(AUTOLOAD);
22  
23 our @ISA = qw(Exporter);
24  
25 our %EXPORT_TAGS = (
26 'all' => [
27 qw(
28  
29 )
30 ]
31 );
32  
33 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
34  
35 our @EXPORT = qw( );
36  
37  
38 sub new {
39  
40 my ( $this, %params ) = @_;
41  
42 my $class = ref($this) || $this;
43 my $self = {};
44 bless $self, $class;
45  
46 if ( exists( $params{path} ) ) {
47 $self->{path} = $params{path};
48 }
49 else {
50 $self->{path} = "~/.ustore";
51 }
52  
53 if ( exists( $params{prefix} ) ) {
54 $self->{prefix} = $params{prefix};
55 }
56 else {
57 $self->{prefix} = "";
58 }
59  
60 if ( exists( $params{depth} ) ) {
61 $self->{depth} = $params{depth};
62 $self->{depth} = 35 if ( $params{depth} > 35 );
63  
64 }
65 else {
66 $self->{depth} = 3;
67 }
68  
69 if ( !( -e $self->{path} ) ) {
70 mkdir( $self->{path} )
71 or die "Unable to create directory : $self->{path}";
72 }
73  
74 return $self;
75 }
76  
77  
78 sub add {
79  
80 my ( $self, $file ) = @_;
81  
82 my ( $uuid, $uuidString );
83 UUID::generate($uuid);
84 UUID::unparse( $uuid, $uuidString );
85  
86 my $SSubDir;
87 my @tempstr = split( //, $uuidString );
88 my @dirtree;
89 for ( 1 .. $self->{depth} ) {
90 push @dirtree, shift @tempstr;
91 }
92 $SSubDir = File::Spec->catdir( $self->{path}, @dirtree );
93  
94 if ( !( -e $SSubDir ) ) {
95  
96 mkpath($SSubDir)
97 or die "Unable to create subdirectories $SSubDir in the store";
98 }
99  
100 my $destStoredFile =
101 File::Spec->catfile( $SSubDir, $self->{prefix} . $uuidString );
102  
103 copy( $file, $destStoredFile )
104 or die "Unable to copy file into ustore as $destStoredFile";
105  
106 return $uuidString;
107 }
108  
109  
110 sub remove {
111  
112 my ( $self, $id ) = @_;
113  
114 my $destStoredFile;
115  
116 if ( !( defined($id) ) ) { return; }
117  
118 my $SSubDir;
119 my @tempstr = split( //, $id );
120 my @dirtree;
121 for ( 1 .. $self->{depth} ) {
122 push @dirtree, shift @tempstr;
123 }
124 $SSubDir = File::Spec->catdir( $self->{path}, @dirtree );
125  
126 $destStoredFile = File::Spec->catfile( $SSubDir, $self->{prefix} . $id );
127  
128 if ( -e $destStoredFile ) {
129  
130 unlink($destStoredFile) or return;
131  
132 }
133 else {
134 return;
135 }
136  
137 }
138  
139  
140 sub get {
141  
142 my ( $self, $id ) = @_;
143  
144 my $destStoredFile;
145  
146 my $SSubDir;
147 my @tempstr = split( //, $id );
148 my @dirtree;
149 for ( 1 .. $self->{depth} ) {
150 push @dirtree, shift @tempstr;
151 }
152 $SSubDir = File::Spec->catdir( $self->{path}, @dirtree );
153  
154 $destStoredFile = File::Spec->catfile( $SSubDir, $self->{prefix} . $id );
155  
156 if ( -e $destStoredFile ) {
157 open( my $fh, '<', $destStoredFile );
158 return *$fh;
159 }
160 else {
161 return;
162 }
163 }
164  
165  
166 sub getPath {
167  
168 my ( $self, $id ) = @_;
169  
170 my $destStoredFile;
171  
172 my $SSubDir;
173 my @tempstr = split( //, $id );
174 my @dirtree;
175 for ( 1 .. $self->{depth} ) {
176 push @dirtree, shift @tempstr;
177 }
178 $SSubDir = File::Spec->catdir( $self->{path}, @dirtree );
179  
180 $destStoredFile = File::Spec->catfile( $SSubDir, $self->{prefix} . $id );
181  
182 if ( -e $destStoredFile ) {
183 return $destStoredFile;
184 }
185 else {
186 return;
187 }
188 }
189  
190 # local Function
191 sub _printPath {
192 my ($self) = @_;
193  
194 return $self->{path};
195  
196 }
197  
198 1;
199  
200 __END__