Revision 128

Date:
2011/08/26 15:52:43
Author:
ahitrov
Revision Log:
Binary store and delete - administration interface
Files:

Legend:

 
Added
 
Removed
 
Modified
  • utf8/core/comps/contenido/components/inputs/multimedia_new.msn

     
    1 1 <!-- Один объект мультимедиа -->
    2 2 <table cellpadding="0" cellspacing="2" border="0" width="100%">
    3 3 <tr><td><b><% $rusname %></b> / <font size="-1" color="#888888">name="<% $attr %>"</font> </td></tr>
    4 <tr><td><input type="file" name="<% $attr %>" style="width:95%;font-family:arial;font-size:9pt;"></td></tr>
    4 <tr><td><a href="javascript:void(0)" class="mm_act" id="<% $attr %>_file_prompt" style="padding:0 6px;"
    5 onclick="$('#<% $attr %>_url_loader').hide(); $('#<% $attr %>_file_loader').show(); $('#<% $attr %>_file_prompt').addClass('mm_act'); $('#<% $attr %>_url_prompt').removeClass('mm_act'); return false;">file</a>
    6 <a href="javascript:void(0)" class="mm_inact" id="<% $attr %>_url_prompt" style="padding:0 6px;"
    7 onclick="$('#<% $attr %>_file_loader').hide(); $('#<% $attr %>_url_loader').show(); $('#<% $attr %>_url_prompt').addClass('mm_act'); $('#<% $attr %>_file_prompt').removeClass('mm_act'); $('#<% $attr %>_ext_url')[0].focus(); return false;">url</a></div>
    8 <div id="<% $attr %>_file_loader" style="background:#d0d0d0; padding:2px;">
    9 <input type="file" name="<% $attr %>" style="width:95%;font-family:arial;font-size:9pt;"></div>
    10 <div id="<% $attr %>_url_loader" style="display:none; background:#d0d0d0; padding:2px;">&nbsp;URL:&nbsp;<input
    11 id="<% $attr %>_ext_url" type="text" name="<% $attr %>.ext_url" style="width:81%;font-family:arial;font-size:9pt;"></div>
    12 </td></tr>
    5 13 % if ( exists($MULTI->{filename}) )
    6 14 % {
    7 15 % my $TYPE = 'unknown';
  • utf8/core/comps/contenido/components/set_multimedia_new.msn

     
    81 81 </%args>
    82 82 <%init>
    83 83
    84 use IO::File;
    85
    86 84 return undef unless defined $SETS and defined $field;
    87 85 #use vars qw($keeper);
    88 86
     
    91 89
    92 90 my $BINARY = $default;
    93 91
    94 if ($SETS->{$field}) {
    95 my $filename = "/binary/".$object->get_file_name() || return;
    96 my $upload = $r->upload($field);
    97 if ( $prop->{softrename} ) {
    98 my $oid = $object->id || int(rand(10000));
    99 my $orig_name = $upload->filename();
    100 if ( $orig_name =~ /\\([^\\]+)$/ ) {
    101 $orig_name = $1;
    102 } elsif ( $orig_name =~ /\/([^\/]+)$/ ) {
    103 $orig_name = $1;
    104 }
    105 $orig_name =~ s/[\ \t]/_/g;
    106 $orig_name = $oid.'_'.$orig_name;
    107 $filename =~ s/\/([^\/]+)$//;
    108 my $fname = $1;
    109 unless ( $orig_name =~ /^[a-zA-Z_\d\.\-\,]+$/ ) {
    110 $orig_name = $m->comp('.translit', str => $orig_name);
    111 }
    112 warn "\n\n\n\n\nNew Name: [$orig_name]\n\n\n\n\n";
    113 unless ( $orig_name =~ /^[a-zA-Z_\d\.\-\,]+$/ ) {
    114 $orig_name = $fname;
    115 }
    116 $filename .= '/'.$orig_name;
    117 $filename =~ s/\.([^\.]+)$//;
    92 if ($SETS->{$field} || $SETS->{"$field.ext_url"}) {
    93
    94 my $upload;
    95 if ( $SETS->{$field} ) {
    96 $upload = $r->upload($field);
    97 } elsif ( $SETS->{"$field.ext_url"} && $SETS->{"$field.ext_url"} =~ /^(http|https|ftp):\/\/.*/ ) {
    98 $upload = $SETS->{"$field.ext_url"};
    118 99 }
    119 my $filename_tmp = $state->{"tmp_dir"}."/".join("_", split("/", $filename));
    100 if ($upload) {
    120 101
    121 my $suffix = $upload->filename() =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
    122 my $fh = $upload->fh();
    123 my $size = (stat $fh)[7];
    102 my $BIN = $object->_store_binary( $upload, attr => $prop->{attr} );
    124 103
    125 my $fh_tmp = IO::File->new(">".$filename_tmp.".".$suffix) || die "Can't open temporary file";
    104 if ( ref $BIN eq 'HASH' && exists $BIN->{filename} ) {
    105 $object->_delete_binary( $default );
    106 %$BINARY = %$BIN;
    107 }
    126 108
    127 my $buffer;
    128
    129 $size = sysread $fh, $buffer, $size;
    130 syswrite $fh_tmp, $buffer, $size;
    131
    132 undef $fh_tmp;
    133
    134 if (Contenido::File::store($filename.".".$suffix, $filename_tmp.".".$suffix)) {
    135 if (ref $BINARY eq "HASH" and $BINARY->{"filename"}) {
    136 Contenido::File::remove($BINARY->{"filename"});
    137 }
    138
    139 @{$BINARY}{"filename", "ext", "size"} = (
    140 $filename.".".$suffix,
    141 $suffix,
    142 $size
    143 );
    109 } else {
    110 warn 'ERROR in incoming POST form: "'.$field.' is not file upload (value: "'.$SETS->{$field}.'")';
    144 111 }
    145 112
    146 unlink $filename_tmp.".".$suffix if -e $filename_tmp.".".$suffix;
    147 if ( $suffix =~ /(rar|7z|zip|arc|lha|arj|cab)/ ) {
    148 $BINARY->{type} = 'archive';
    149 } elsif ( $suffix =~ /(doc|rtf)/ ) {
    150 $BINARY->{type} = 'doc';
    151 } elsif ( $suffix eq 'xls' ) {
    152 $BINARY->{type} = 'xls';
    153 } elsif ( $suffix =~ /(mdb|ppt)/ ) {
    154 $BINARY->{type} = 'msoffice';
    155 } elsif ( $suffix =~ /(pdf)/ ) {
    156 $BINARY->{type} = 'ebook';
    157 } elsif ( $suffix eq 'psd' ) {
    158 $BINARY->{type} = 'psd';
    159 } elsif ( $suffix =~ /(exe|msi|cab)/ ) {
    160 $BINARY->{type} = 'executable';
    161 } else {
    162 $BINARY->{type} = 'unknown';
    163 }
    164 113 }
    165 114
    166 115 # Дополнительные поля - берем все, что найдем...
     
    169 118
    170 119 foreach my $localfield (keys(%{ $SETS })) {
    171 120 if ($localfield =~ /$pattern/) {
    172 my $imagefield = $1;
    173 $BINARY->{$imagefield} = $m->comp("/contenido/components/filter.msn", str => $SETS->{$localfield});
    121 my $imagefield = $1;
    122 $BINARY->{$imagefield} = $m->comp("/contenido/components/filter.msn", str => $SETS->{$localfield});
    174 123 }
    175 124 }
    176 125
  • utf8/core/lib/Contenido/File.pm

     
    14 14
    15 15 our $IgnoreErrors = 1;
    16 16
    17 my %translit = (
    18 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y',
    19 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h',
    20 'ц' => 'ts', 'ч' => '4', 'ш' => 'sh', 'щ' => 'sch', 'ъ' => 'y', 'ы' => 'i', 'ь' => 'y', 'э' => 'e', 'ю' => 'u', 'я' => 'a', 'А' => 'A',
    21 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'E', 'Ж' => 'ZH', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L',
    22 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'TS', 'Ч' => '4',
    23 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => 'Y', 'Ы' => 'I', 'Ь' => 'Y', 'Э' => 'E', 'Ю' => 'U', 'Я' => 'YA',
    24 );
    25
    26
    17 27 sub fetch {
    18 28 my $filename = shift || return;
    19 29 my $fh;
     
    337 347 return unless ref $prop;
    338 348
    339 349 my $filename = '/binary/'.$object->get_file_name() || return;
    350 if ( $prop->{softrename} ) {
    351 my $oid = $object->id || int(rand(10000));
    352 my $orig_name = '';
    353 if ( ref $input eq 'Apache::Upload' ) {
    354 $orig_name = $input->filename();
    355 } elsif ( !ref $input ) {
    356 $orig_name = $input;
    357 }
    358 if ( $orig_name ) {
    359 if ( $orig_name =~ /\\([^\\]+)$/ ) {
    360 $orig_name = $1;
    361 } elsif ( $orig_name =~ /\/([^\/]+)$/ ) {
    362 $orig_name = $1;
    363 }
    364 $orig_name =~ s/[\ \t]/_/g;
    365 $orig_name = $oid.'_'.$orig_name;
    366 $filename =~ s/\/([^\/]+)$//;
    367 my $fname = $1;
    368 unless ( $orig_name =~ /^[a-zA-Z_\d\.\-\,]+$/ ) {
    369 $orig_name = translit( $orig_name );
    370 }
    371 warn "\n\n\n\n\nNew Name: [$orig_name]\n\n\n\n\n" if $DEBUG;
    372 unless ( $orig_name =~ /^[a-zA-Z_\d\.\-\,]+$/ ) {
    373 $orig_name = $fname;
    374 }
    375 $filename .= '/'.$orig_name;
    376 $filename =~ s/\.([^\.]+)$//;
    377 }
    378 }
    379
    340 380 my $filename_tmp = $state->{'tmp_dir'}.'/'.join('_', split('/', $filename));
    341 381
    342 382 my $fh = get_fh($input);
     
    345 385 my $ext;
    346 386 my $size = 1073741824;
    347 387 if ( not ref $input ) {
    348 $ext = $input =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
    388 $ext = $input =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
    349 389 if ( scheme($input) eq 'file' ) {
    350 390 $size = (stat $fh)[7];
    351 391 }
    352 392 } elsif ( ref $input eq 'Apache::Upload' ) {
    353 $ext = $input->filename() =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
    393 $ext = $input->filename() =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
    354 394 $size = (stat $fh)[7];
    355 395 } elsif ( $opts{filename} ) {
    356 $ext = $opts{filename} =~ /(jpe?g|gif|png)$/i ? lc $1 : 'bin';
    396 $ext = $opts{filename} =~ /\.([^\.]+)$/ ? lc($1) : 'bin';
    357 397 }
    358 398 if ( ref $fh eq 'IO::Scalar' ) {
    359 399 $size = length("$fh");
     
    370 410
    371 411 my $BINARY;
    372 412 if ( store($filename.'.'.$ext, $filename_tmp.'.'.$ext) ) {
    373 $BINARY = { filename => $filename.'.'.$ext };
    413 @{$BINARY}{"filename", "ext", "size"} = (
    414 $filename.".".$ext, $ext, $size
    415 );
    416
    374 417 unlink $filename_tmp.'.'.$ext if -e $filename_tmp.'.'.$ext;
    418
    419 if ( $ext =~ /(rar|7z|zip|arc|lha|arj|cab)/ ) {
    420 $BINARY->{type} = 'archive';
    421 } elsif ( $ext =~ /(doc|rtf)/ ) {
    422 $BINARY->{type} = 'doc';
    423 } elsif ( $ext eq 'xls' ) {
    424 $BINARY->{type} = 'xls';
    425 } elsif ( $ext =~ /(mdb|ppt)/ ) {
    426 $BINARY->{type} = 'msoffice';
    427 } elsif ( $ext =~ /(pdf)/ ) {
    428 $BINARY->{type} = 'ebook';
    429 } elsif ( $ext eq 'psd' ) {
    430 $BINARY->{type} = 'psd';
    431 } elsif ( $ext =~ /(exe|msi|cab)/ ) {
    432 $BINARY->{type} = 'executable';
    433 } else {
    434 $BINARY->{type} = 'unknown';
    435 }
    436
    375 437 }
    376 438
    377 439 return $BINARY;
     
    388 450 }
    389 451
    390 452
    453 sub translit {
    454 my $str = shift;
    455 my @str = split (//, $str);
    456 my $res = '';
    457 while ( scalar @str ) {
    458 my $alpha = shift @str;
    459 if ( exists $translit{$alpha} ) {
    460 $res .= $translit{$alpha};
    461 } else {
    462 $res .= $alpha;
    463 }
    464 }
    465 return $res;
    466 }
    467
    468
    391 469 1;