blib/lib/Bio/Graphics/Browser2/UserTracks/Filesystem.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 23 totaltotal 0.0% 0 / 225 CC 15 SCAR 23.4
CC
15
Cov
0%
get_uploaded_files
30.0
get_imported_files
30.0
file_type
20.0
CRAP 10.4
1 package Bio::Graphics::Browser2::UserTracks::Filesystem;
2  
3 # $Id: Filesystem.pm 23607 2010-07-30 17:34:25Z cnvandev $
4 use strict;
5 use base 'Bio::Graphics::Browser2::UserTracks';
6 use Bio::Graphics::Browser2::UserTracks;
7 use Bio::Graphics::Browser2;
8 use File::Spec;
9 use File::Path 'rmtree';
10 use Cwd;
11 use Carp "cluck",'croak';
12  
13 # Filesystem works on the basis of a file-based database with the following structure:
14 # base -- e.g. /var/tmp/gbrowse2/userdata
15 # uploadid -- e.g. dc39b67fb5278c0da0e44e9e174d0b40
16 # source -- e.g. volvox
17 # concatenated path /var/tmp/gbrowse2/userdata/volvox/dc39b67fb5278c0da0e44e9e174d0b40
18  
19 # The concatenated path contains a series of directories named after the track.
20 # Each directory has a .conf file that describes its contents and configuration.
21 # There will also be data files associated with the configuration.
22  
23 # Get Uploaded Files (User) - Returns an array of the paths of files owned by a user.
24 sub get_uploaded_files {
25 my $self = shift;
26 return unless $self->{uploadsid};
27 my $path = $self->path;
28 return unless -e $path;
29 my @result;
30 opendir D, $path;
31 while (my $dir = readdir(D)) {
32 next if $dir =~ /^\.+$/;
33 next if ($self->is_imported($dir) == 1);
34 push @result, $dir;
35 }
36 return @result;
37 }
38  
39 # Get Imported Files (User) - Returns an array of files imported by a user.
40 sub get_imported_files {
41 my $self = shift;
42 return unless $self->{uploadsid};
43 my $path = $self->path;
44 return unless -e $path;
45 my @result;
46 opendir D, $path;
47 while (my $dir = readdir(D)) {
48 next if $dir =~ /^\.+$/;
49 next if ($self->is_imported($dir) == 0);
50 push @result, $dir;
51 }
52 return @result;
53 }
54  
55 sub get_track_upload_id {
56 my $self = shift;
57 my $uploadsid = $self->{uploadsid};
58 my $id = $self->SUPER::get_track_upload_id(@_);
59 return "$uploadsid:$id";
60 }
61  
62 # File Exists (Full Path[, Owner]) - Returns the number of results for a file, 0 if not found.
63 sub file_exists {
64 my $self = shift;
65 my $path = shift;
66 return (-e $path);
67 }
68  
69 # Add File - A placeholder function while UserTracks holds the file uploading bit.
70 sub add_file {
71 my $self = shift;
72 my $filename = shift;
73
74 my %track_lookup = $self->track_lookup;
75 $track_lookup{$_} = $filename foreach $self->labels($filename);
76
77 return $filename;
78 }
79  
80 sub share_link {
81 my $self = shift;
82 my $file = shift or die "No input or invalid input given to share()";
83 return $self->share($file);
84 }
85  
86 sub share {
87 my $self = shift;
88 my $fileid = shift;
89 my ($uploadsid,$filename) = split ':',$fileid,2;
90 my %track_lookup = $self->track_lookup;
91 my $label = 'track_'.substr($uploadsid,0,6).'_'.$filename;
92 $track_lookup{$label} = "$uploadsid/$filename";
93  
94 # add to user's session
95 my $page_settings = $self->page_settings;
96 $page_settings->{shared_files}{$label} = $track_lookup{$label};
97 $self->session->flush;
98 }
99  
100 sub unshare {
101 my $self = shift;
102 my $file = shift or croak "No input or invalid input given to unshare()";
103 my $page_settings = $self->page_settings;
104 delete $page_settings->{shared_files}{$file};
105 $self->session->flush;
106 }
107  
108 # Delete File - Pretty self-explanatory.
109 sub delete_file {
110 my $self = shift;
111 my $file = shift;
112
113 my %track_lookup = $self->track_lookup;
114 delete $track_lookup{$_} foreach $self->labels($file);
115
116 my $loader = Bio::Graphics::Browser2::DataLoader->new($file,
117 $self->track_path($file),
118 $self->track_conf($file),
119 $self->{config},
120 $self->{uploadsid});
121 $loader->drop_databases($self->track_conf($file));
122 rmtree($self->track_path($file));
123 }
124 sub clone_database() { } # do nothing
125 sub get_added_public_files { return }
126 sub get_shared_files {
127 my $self = shift;
128 my $settings = $self->page_settings;
129 my $shared_files = $settings->{shared_files} or return;
130 return keys %$shared_files;
131 }
132  
133 # Created (File) - Returns creation date of $track.
134 sub created {
135 my $self = shift;
136 my $file = shift;
137 my $conf = $self->track_conf($file);
138 return (stat($conf))[10];
139 }
140  
141 # Modified (File) - Returns date modified of $track.
142 sub modified {
143 my $self = shift;
144 my $file = shift;
145 return ($self->conf_metadata($file))[1];
146 }
147  
148 # Description (File[, Description]) - Returns a file's description, or changes the current description if defined.
149 sub description {
150 my $self = shift;
151 my $file = shift;
152 my $filename = $self->escape_url($file);
153 my $desc = File::Spec->catfile($self->track_path($file), "$filename.desc");
154 if (@_) {
155 open my $f,">",$desc or return;
156 print $f join("\n",@_);
157 close $f;
158 return 1;
159 } else {
160 open my $f,"<",$desc or return;
161 my @lines = <$f>;
162 return join '',@lines;
163 }
164 }
165  
166 # Is Imported (File) - Returns 1 if an already-added track is imported, 0 if not.
167 sub is_imported {
168 my $self = shift;
169 my $file = shift;
170 return (-e File::Spec->catfile($self->track_path($file), $self->imported_file_name))? 1 : 0;
171 }
172  
173 # File Type (File) - Returns the type of a specified track.
174 sub file_type {
175 my $self = shift;
176 my $file = shift;
177 return !$self->is_mine($file) ? 'shared'
178 :$self->is_imported($file) ? 'imported'
179 :$self->is_mirrored($file) ? 'imported'
180 :'uploaded';
181 }
182  
183 # Filename (File) - Returns the filename - is used basically in contrast with Database.pm's filename function, which is more involved.
184 sub filename {
185 my $self = shift;
186 my $file = shift;
187 if (my $shared_files = $self->page_settings->{shared_files}) {
188 return $shared_files->{$file} || $file;
189 } else {
190 return $file;
191 }
192 }
193  
194 # Is Mine (File) - Returns if a file belongs to the logged-in user. Since this only works with logged-in users, is always true.
195 sub is_mine {
196 my $self = shift;
197 my $file = shift;
198 return !exists $self->page_settings->{shared_files}{$file};
199 }
200  
201 # Owner (File) - Returns the owner of a file. It's basically used in contrast with Database.pm's owner function.
202 sub owner {
203 return shift->{uploadsid};
204 }
205  
206 # Title (File) - Returns the title of a file, which is the filename.
207 sub title {
208 my $self = shift;
209 my $filename = $self->filename(shift);
210 $filename =~ s!^[a-f0-9]{32}/!!;
211 return $filename;
212 }
213  
214 # Get File ID (File) - Returns the ID of a file, which is the filename.
215 sub get_file_id {
216 return shift->filename(shift);
217 }
218  
219 sub owner_name {
220 return "you";
221 }
222  
223 1;