Revision 1dfb0798
Von Werner Hahn vor mehr als 7 Jahren hinzugefügt
SL/Controller/FileUploader.pm | ||
---|---|---|
package SL::Controller::FileUploader;
|
||
|
||
use strict;
|
||
use parent qw(SL::Controller::Base);
|
||
|
||
use SL::DB::File;
|
||
|
||
use SL::Helper::Flash;
|
||
use SL::Locale::String;
|
||
|
||
use Rose::Object::MakeMethods::Generic
|
||
(
|
||
'scalar --get_set_init' => [ qw(file) ],
|
||
);
|
||
|
||
#
|
||
# actions
|
||
#
|
||
sub action_test_page{
|
||
my ($self) = @_;
|
||
$self->render('fileuploader/test_page');
|
||
}
|
||
|
||
sub action_upload_form{
|
||
my ($self) = @_;
|
||
$self->file(SL::DB::File->new);
|
||
$self->render('common/file_upload', {header => 0});
|
||
}
|
||
|
||
sub action_show_files {
|
||
my ($self) = @_;
|
||
|
||
|
||
}
|
||
|
||
sub action_ajax_add_file{
|
||
my ($self) = @_;
|
||
$self->file( $::form->{id} ? SL::DB::File->new(id => $::form->{id})->load : SL::DB::File->new );
|
||
$self->render('common/file_upload', { layout => 0}, data => $::form);
|
||
}
|
||
|
||
|
||
#
|
||
# helpers
|
||
#
|
||
|
||
sub validate_filetype {
|
||
my ($self,$filename,$allowed_filetypes) = @_;
|
||
|
||
my @errors;
|
||
my($file,$filetype) = split /\./, $filename;
|
||
my @file_types = split /\|/, $allowed_filetypes;
|
||
|
||
if (!grep {$_ eq $filetype} @file_types) {
|
||
push @errors, t8("Filetype not allowed");
|
||
}
|
||
return @errors
|
||
}
|
||
|
||
sub action_upload_form{
|
||
my ($self) = @_;
|
||
$self->file(SL::DB::File->new);
|
||
$self->render('common/file_upload', {header => 0});
|
||
}
|
||
|
||
sub action_show_files {
|
||
my ($self) = @_;
|
||
|
||
|
||
sub action_ajax_add_file{
|
||
my ($self) = @_;
|
||
$self->file(SL::DB::File->new);
|
||
$self->render('common/file_upload', { layout => 0}, DATA => $::form);
|
||
}
|
||
|
||
sub action_ajax_upload_file{
|
||
my ($self, %params) = @_;
|
||
my $attributes = $::form->{ $::form->{form_prefix} } || die "Missing FormPrefix";
|
||
$attributes->{trans_id} = $::form->{id} || die "Missing ID";
|
||
$attributes->{modul} = $::form->{modul} || die "Missing Modul";
|
||
$attributes->{filename} = $::form->{FILENAME} || die "Missing Filename";
|
||
$attributes->{title} = $::form->{ $::form->{form_prefix} }->{title};
|
||
$attributes->{description} = $::form->{ $::form->{form_prefix} }->{description};
|
||
my @image_types = ("jpg","tif","png");
|
||
|
||
my @errors = $self->file(SL::DB::File->new(%{ $attributes }))->validate;
|
||
return $self->js->error(@errors)->render($self) if @errors;
|
||
|
||
$self->file->save;
|
||
|
||
# TODO js call
|
||
$self->render('fileuploader/test_page');
|
||
}
|
||
|
||
#
|
||
# helpers
|
||
#
|
||
|
||
sub init_file {
|
||
return SL::DB::File->new(id => $::form->{id})->load;
|
||
}
|
||
|
||
1;
|
||
__END__
|
||
|
||
=pod
|
||
|
||
=encoding utf8
|
||
|
||
=head1 NAME
|
||
|
||
SL::Controller::FileUploader - Controller to manage fileuploads
|
||
|
||
=head1 SYNOPSIS
|
||
|
||
use SL::Controller::FileUploader;
|
||
|
||
# synopsis.. =>
|
||
|
||
=head1 DESCRIPTION
|
||
|
||
# longer description...
|
||
|
||
|
||
=head1 INTERFACE
|
||
|
||
|
||
=head1 DEPENDENCIES
|
||
|
||
|
||
=head1 SEE ALSO
|
||
|
||
=head1 AUTHOR
|
||
|
||
Werner Hahn E<lt>wh@futureworldsearch.netE<gt>
|
||
|
||
=cut
|
SL/DB/Helper/ThumbnailCreator.pm | ||
---|---|---|
package SL::DB::Helper::ThumbnailCreator;
|
||
|
||
use strict;
|
||
|
||
use Carp;
|
||
use GD;
|
||
use Image::Info;
|
||
use File::MimeInfo::Magic;
|
||
use List::MoreUtils qw(apply);
|
||
use List::Util qw(max);
|
||
use Rose::DB::Object::Util;
|
||
|
||
require Exporter;
|
||
our @ISA = qw(Exporter);
|
||
our @EXPORT = qw(file_create_thumbnail file_update_thumbnail file_probe_type file_update_type_and_dimensions);
|
||
|
||
# TODO PDFs and others like odt,txt,...
|
||
our %supported_mime_types = (
|
||
'image/gif' => { extension => 'gif', convert_to_png => 1, },
|
||
'image/png' => { extension => 'png' },
|
||
'image/jpeg' => { extension => 'jpg' },
|
||
'image/tiff' => { extension => 'tif'},
|
||
);
|
||
|
||
sub file_create_thumbnail {
|
||
my ($self) = @_;
|
||
croak "No picture set yet" if !$self->file_content;
|
||
|
||
my $image = GD::Image->new($self->file_content);
|
||
my ($width, $height) = $image->getBounds;
|
||
my $max_dim = 64;
|
||
my $curr_max = max $width, $height, 1;
|
||
my $factor = $curr_max <= $max_dim ? 1 : $curr_max / $max_dim;
|
||
my $new_width = int($width / $factor + 0.5);
|
||
my $new_height = int($height / $factor + 0.5);
|
||
my $thumbnail = GD::Image->new($new_width, $new_height);
|
||
|
||
$thumbnail->copyResized($image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
|
||
|
||
$self->thumbnail_img_content($thumbnail->png);
|
||
$self->thumbnail_img_content_type('image/png');
|
||
$self->thumbnail_img_width($new_width);
|
||
$self->thumbnail_img_height($new_height);
|
||
return 1;
|
||
|
||
}
|
||
|
||
sub file_update_thumbnail {
|
||
my ($self) = @_;
|
||
|
||
return 1 if !$self->file_content || !$self->file_content_type || !Rose::DB::Object::Util::get_column_value_modified($self, 'file_content');
|
||
$self->file_create_thumbnail;
|
||
return 1;
|
||
}
|
||
|
||
sub file_probe_type {
|
||
my ($self) = @_;
|
||
|
||
return (t8("No file uploaded yet")) if !$self->file_content;
|
||
my $mime_type = File::MimeInfo::Magic::magic($self->file_content);
|
||
|
||
my $info = Image::Info::image_info(\$self->{file_content});
|
||
if (!$info || $info->{error} || !$info->{file_media_type} || !$supported_mime_types{ $info->{file_media_type} }) {
|
||
$::lxdebug->warn("Image::Info error: " . $info->{error}) if $info && $info->{error};
|
||
return (t8('Unsupported image type (supported types: #1)', join(' ', sort keys %supported_mime_types)));
|
||
}
|
||
|
||
$self->file_content_type($info->{file_media_type});
|
||
$self->files_img_width($info->{width});
|
||
$self->files_img_height($info->{height});
|
||
$self->files_mtime(DateTime->now_local);
|
||
|
||
$self->file_create_thumbnail;
|
||
|
||
return ();
|
||
}
|
||
|
||
sub file_update_type_and_dimensions {
|
||
my ($self) = @_;
|
||
|
||
return () if !$self->file_content;
|
||
return () if $self->file_content_type && $self->file_img_width && $self->file_img_height && !Rose::DB::Object::Util::get_column_value_modified($self, 'file_content');
|
||
|
||
my @errors = $self->file_probe_type;
|
||
return @errors if @errors;
|
||
|
||
my $info = $supported_mime_types{ $self->file_content_type };
|
||
if ($info->{convert_to_png}) {
|
||
$self->file_content(GD::Image->new($self->file_content)->png);
|
||
$self->file_content_type('image/png');
|
||
$self->filename(apply { s/\.[^\.]+$//; $_ .= '.png'; } $self->filename);
|
||
}
|
||
return ();
|
||
}
|
||
|
||
1;
|
||
__END__
|
||
|
||
=pod
|
||
|
||
=encoding utf8
|
||
|
||
=head1 NAME
|
||
|
||
SL::DB::Helper::ThumbnailCreator - DatabaseClass Helper for Fileuploads
|
||
|
||
=head1 SYNOPSIS
|
||
|
||
use SL::DB::Helper::ThumbnailCreator;
|
||
|
||
# synopsis...
|
||
|
||
=head1 DESCRIPTION
|
||
|
||
# longer description..
|
||
=head1 AUTHOR
|
||
|
||
Werner Hahn E<lt>wh@futureworldsearch.netE<gt>
|
||
|
||
=cut
|
||
|
||
|
||
=head1 INTERFACE
|
||
|
||
|
||
=head1 DEPENDENCIES
|
||
|
||
|
||
=head1 SEE ALSO
|
||
|
||
|
sql/Pg-upgrade2/files.sql | ||
---|---|---|
-- @tag: files
|
||
-- @description: Tabelle für Files
|
||
-- @charset: UTF-8
|
||
-- @depends: release_3_4_1
|
||
-- @depends: release_3_4_1
|
||
CREATE TABLE files(
|
||
id SERIAL PRIMARY KEY,
|
||
object_type TEXT NOT NULL, -- Tabellenname des Moduls z.B. customer, parts ... Fremdschlüssel Zusammen mit object_id
|
||
object_id INTEGER NOT NULL, -- Fremdschlüssel auf die id der Tabelle aus Spalte object_type
|
||
file_name TEXT NOT NULL,
|
||
file_type TEXT NOT NULL,
|
||
file_name TEXT NOT NULL,
|
||
file_type TEXT NOT NULL,
|
||
mime_type TEXT NOT NULL,
|
||
source TEXT NOT NULL,
|
||
source TEXT NOT NULL,
|
||
backend TEXT,
|
||
backend_data TEXT,
|
||
backend_data TEXT,
|
||
title varchar(45),
|
||
description TEXT,
|
||
description TEXT,
|
||
itime TIMESTAMP DEFAULT now(),
|
||
mtime TIMESTAMP,
|
||
CONSTRAINT valid_type CHECK (
|
||
(object_type = 'credit_note') OR (object_type = 'invoice') OR (object_type = 'sales_order') OR (object_type = 'sales_quotation')
|
||
OR (object_type = 'sales_delivery_order') OR (object_type = 'request_quotation') OR (object_type = 'purchase_order')
|
||
OR (object_type = 'purchase_delivery_order') OR (object_type = 'purchase_invoice')
|
||
OR (object_type = 'vendor') OR (object_type = 'customer') OR (object_type = 'part') OR (object_type = 'gl_transaction')
|
||
OR (object_type = 'purchase_delivery_order') OR (object_type = 'purchase_invoice')
|
||
OR (object_type = 'vendor') OR (object_type = 'customer') OR (object_type = 'part') OR (object_type = 'gl_transaction')
|
||
OR (object_type = 'dunning') OR (object_type = 'dunning1') OR (object_type = 'dunning2') OR (object_type = 'dunning3')
|
||
OR (object_type = 'draft') OR (object_type = 'statement'))
|
||
);
|
||
|
templates/webpages/common/file_upload.html | ||
---|---|---|
[%- USE LxERP -%][%- USE L -%][%- USE HTML -%][%- USE JavaScript -%][% USE Base64 %]
|
||
[% SET style="width: 500px" %]
|
||
[% SET id_base = "fileupload" %]
|
||
[% L.dump(DATA) %]
|
||
<form method="post" id="fileupload_form" method="POST" enctype="multipart/form-data">
|
||
[% L.hidden_tag('form_prefix', id_base, id=id_base _ '_form_prefix') %]
|
||
[% L.hidden_tag('id', DATA.id, no_id=1) %]
|
||
[% L.hidden_tag('modul', DATA.modul) %]
|
||
|
||
<h2>
|
||
[%- IF SELF.file.id %]
|
||
[%- LxERP.t8("Edit file properties ", SELF.file.number) %]
|
||
[%- ELSE %]
|
||
[%- LxERP.t8("Add file to webdav") %]
|
||
[%- END %]
|
||
</h2>
|
||
|
||
<table>
|
||
[% IF SELF.file.number %]
|
||
<tr>
|
||
<th align="right">[%- LxERP.t8("Number") %]:</th>
|
||
<td>[% HTML.escape(SELF.file.number) %]</td>
|
||
</tr>
|
||
[% END %]
|
||
|
||
<tr>
|
||
<th align="right">[%- LxERP.t8("Description") %]:</th>
|
||
<td>[% L.input_tag(id_base _ '.description', SELF.file.description, style=style) %]</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<th align="right">[%- LxERP.t8("Title") %]:</th>
|
||
<td>[% L.input_tag(id_base _ '.title', SELF.file.title, style=style) %]</td>
|
||
</tr>
|
||
|
||
[% IF SELF.file.file_content %]
|
||
<tr>
|
||
<th align="right">[%- LxERP.t8("File name") %]:</th>
|
||
<td>[% HTML.escape(SELF.file.file_file_name) %]</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<th align="right">[%- LxERP.t8("MIME type") %]:</th>
|
||
<td>[% HTML.escape(SELF.file.file_content_type) %]</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<th align="right">[%- LxERP.t8("Dimensions") %]:</th>
|
||
<td>[% HTML.escape(SELF.file.file_width) %]x[% HTML.escape(SELF.file.file_height) %]</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<th align="right">[%- LxERP.t8("Uploaded at") %]:</th>
|
||
<td>[% HTML.escape(SELF.file.file_mtime.to_kivitendo(precision='second')) %]</td>
|
||
</tr>
|
||
[% END %]
|
||
|
||
<tr>
|
||
<th align="right">[%- LxERP.t8("Select file to upload") %]:</th>
|
||
<td>[% L.input_tag(id_base _ '.file_content', '', type='file') %]</td>
|
||
</tr>
|
||
</table>
|
||
|
||
<p>
|
||
[%- L.ajax_submit_tag('controller.pl?action=FileUploader/ajax_upload_file', '#fileupload_form', LxERP.t8('Save'), no_id=1) %]
|
||
<a href="#" onclick="$('#jqueryui_popup_dialog').dialog('close');">[%- LxERP.t8("Cancel") %]</a>
|
||
</p>
|
||
|
||
</form>
|
||
|
||
[% IF SELF.file.id %]
|
||
<h2>[% LxERP.t8("Current file") %]</h2>
|
||
|
||
<div>
|
||
<img src="data:[% HTML.escape(SELF.file.file_content_type) %];base64,[% SELF.file.file_content.encode_base64 %]">
|
||
</div>
|
||
[% END %]
|
templates/webpages/fileuploader/test_page.html | ||
---|---|---|
[% USE L %]
|
||
|
||
<h1>Fileupload Testpage</h1>
|
||
<form method="post" id="fileupload" method="POST" enctype="multipart/form-data">
|
||
<div id="testfunctions" style="width:50%; float:left; border:thin solid green">
|
||
<div id="part_1" height="75px">
|
||
Part: Select Part Where to store the file<br>
|
||
[% L.part_picker('part_id1','',fat_set_item=1) %]<br>
|
||
Part: <span id="change1"> </span> -- <span id="change2"></span><br>
|
||
<div id="fileupload_button" style="display:none;">
|
||
[% L.hidden_tag("id") %]
|
||
[% L.hidden_tag("modul","part") %]
|
||
[% L.button_tag("add_file(this.form.id.value,this.form.modul.value)", 'Fileupload') %]
|
||
</div>
|
||
</div>
|
||
<div id="shoppart" height="75px">
|
||
ShopPart: Select Part Where to store the file<br>
|
||
[% L.part_picker('shoppart','',fat_set_item=1) %]<br>
|
||
ShopPart: <span id="shoppartchange1"> </span> -- <span id="shoppartchange2"></span><br>
|
||
<div id="shop_img_upload_button" style="display:none;">
|
||
[% L.hidden_tag("shop_part_id") %]
|
||
[% L.hidden_tag("shop_part_modul","shop_part") %]
|
||
[% L.button_tag("add_file(this.form.shop_part_id.value,this.form.shop_part_modul.value)", 'Fileupload') %]
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</form>
|
||
|
||
<div id="testviews" style="width:50%; float:left; border:thin solid green">
|
||
<div id="viewpart" height="75px">
|
||
</div>
|
||
<div id="viewshoppart" height="75px">
|
||
</div>
|
||
</div>
|
||
<script type='text/javascript'>
|
||
<!--
|
||
$('#part_id1').change(function() { $('#change1').html($('#part_id1').val()) });
|
||
$('#part_id1').on('set_item:PartPicker', function(e,o) { $('#id').val(o.id);
|
||
$('#change2').html(o.id);
|
||
$('#fileupload_button').show();
|
||
});
|
||
$('#shoppart').change(function() { $('#shoppartchange1').html($('#shoppart').val()) });
|
||
$('#shoppart').on('set_item:PartPicker', function(e,o) { $('#shop_part_id').val(o.id);
|
||
$('#shoppartchange2').html(o.id);
|
||
$('#shop_img_upload_button').show();
|
||
});
|
||
function add_file(id,modul) {
|
||
//var id = this.$('#part').val();
|
||
kivi.popup_dialog({
|
||
url : 'controller.pl?action=FileUploader/ajax_add_file',
|
||
data: 'id=' + id + '&modul=' + modul,
|
||
dialog: { title: kivi.t8('File upload') }
|
||
} );
|
||
return true;
|
||
}
|
||
-->
|
||
</script>
|
Auch abrufbar als: Unified diff
Shopmodul: Fileuploader