| 1 | package Crop::File; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Crop::File - File utilities for the Crop framework | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Crop::File; | ||
| 12 | # ...usage... | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Crop::File provides file-related utilities for the Crop framework. | ||
| 17 | |||
| 18 | =head1 AUTHORS | ||
| 19 | |||
| 20 | Euvgenio (Core Developer) | ||
| 21 | |||
| 22 | Alex (Contributor) | ||
| 23 | |||
| 24 | =head1 COPYRIGHT AND LICENSE | ||
| 25 | |||
| 26 | Apache 2.0 | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use base qw/ Crop::Object::Simple /; | ||
| 31 | |||
| 32 | =begin nd | ||
| 33 | Class: Crop::File | ||
| 34 | File for project. | ||
| 35 | |||
| 36 | Abstract class. | ||
| 37 | =cut | ||
| 38 | |||
| 39 | use v5.14; | ||
| 40 | use warnings; | ||
| 41 | |||
| 42 | use Crop::Error; | ||
| 43 | use Crop::File::Warehouse; | ||
| 44 | use Crop::File::LOCAL; | ||
| 45 | use Crop::File::CDN; | ||
| 46 | use Crop::Util; | ||
| 47 | |||
| 48 | use Crop::Debug; | ||
| 49 | |||
| 50 | =begin nd | ||
| 51 | Variable: my $Type | ||
| 52 | Direct hash of types in form of (1=>LOCAL, 2=>CDN,...). | ||
| 53 | |||
| 54 | The point is this structure is calculated only once. | ||
| 55 | |||
| 56 | Variable: my %WH_type | ||
| 57 | Reverse hash of Types in form of (LOCAL=>1, CDN=>2,...). | ||
| 58 | |||
| 59 | The point is this structure is calculated only once. | ||
| 60 | =cut | ||
| 61 | my $Type = Crop::File::Warehouse->All->Hash; | ||
| 62 | my %WH_type; | ||
| 63 | while (my ($id, $wh) = each %$Type) { | ||
| 64 | $WH_type{$wh->code} = $id; | ||
| 65 | } | ||
| 66 | |||
| 67 | =begin nd | ||
| 68 | Variable: our %Attributes | ||
| 69 | Class attributes: | ||
| 70 | |||
| 71 | ctime - creation time of file on site, not the ctime of file themself | ||
| 72 | id - generated by <Crop::Object::Simple> | ||
| 73 | path - full path in warehouse, including filename, without leading slash | ||
| 74 | size - size in bytes | ||
| 75 | warehouse - warehouse the file is stored in (descendant of this class) | ||
| 76 | =cut | ||
| 77 | our %Attributes = ( | ||
| 78 | ctime => undef, | ||
| 79 | path => {mode => 'read'}, | ||
| 80 | size => {mode => 'read'}, | ||
| 81 | warehouse => undef, | ||
| 82 | ); | ||
| 83 | |||
| 84 | =begin nd | ||
| 85 | Constructor: new (%attr) | ||
| 86 | Set filetype based on the 'warehouse' attr. | ||
| 87 | |||
| 88 | The attribute 'warehouse' serves as the base for define integer key for the sama attribute, and will be replaced the one. | ||
| 89 | |||
| 90 | As this class is abstract direct call arise the error. | ||
| 91 | |||
| 92 | Parameters: | ||
| 93 | %attr - attributes has have at least one obligatory element 'warehouse' in form of CODE (LOCAL, CDN, DAV) | ||
| 94 | |||
| 95 | %attr must be prepared by descendant. | ||
| 96 | |||
| 97 | Returns: | ||
| 98 | $self - ok | ||
| 99 | undef - error | ||
| 100 | =cut | ||
| 101 | sub new { | ||
| 102 | my $class = shift; | ||
| 103 | my $attr = expose_hashes \@_; | ||
| 104 | |||
| 105 | if ($class eq __PACKAGE__) { | ||
| 106 | return warn 'OBJECT|CRIT: File Constructor must have the warehouse been specified' unless defined $attr->{warehouse}; | ||
| 107 | |||
| 108 | my $descendant = 'Crop::File::' . $Type->{$attr->{warehouse}}->code; | ||
| 109 | return $descendant->new($attr, warehouse => undef); | ||
| 110 | } | ||
| 111 | |||
| 112 | return warn 'OBJECT|CRIT: File type is missing' unless $attr->{warehouse}; | ||
| 113 | |||
| 114 | my $self = $class->SUPER::new( | ||
| 115 | %$attr, | ||
| 116 | warehouse => $WH_type{$attr->{warehouse}}, | ||
| 117 | ); | ||
| 118 | $self->{ctime} //= $self->S->tik->timestamp; | ||
| 119 | |||
| 120 | $self; | ||
| 121 | } | ||
| 122 | |||
| 123 | =begin nd | ||
| 124 | Method: Table ( ) | ||
| 125 | Table in the Warehouse. | ||
| 126 | |||
| 127 | Returns: | ||
| 128 | 'file' string | ||
| 129 | =cut | ||
| 130 | sub Table { 'file' } | ||
| 131 | |||
| 132 | =begin nd | ||
| 133 | Method: upload ( ) | ||
| 134 | Pure virtual. | ||
| 135 | |||
| 136 | Uploading process depends on warehouse type, so each type has to define its own mechanism. | ||
| 137 | =cut | ||
| 138 | sub upload { | ||
| 139 | my $self = shift; | ||
| 140 | my $class = ref $self; | ||
| 141 | |||
| 142 | return warn "FILE|CRIT: Method upload() must be redefined by subclass"; | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; |