- die 'no id' unless $params{id};
- my $dbfile = SL::DB::Manager::File->get_first(query => [id => $params{id}]);
- die 'not found' unless $dbfile;
- $main::lxdebug->message(LXDebug->DEBUG2(), "object_id=".$dbfile->object_id." object_type=".$dbfile->object_type." dbfile=".$dbfile);
- SL::File::Object->new(db_file => $dbfile, id => $dbfile->id, loaded => 1);
+ croak "no id or dbfile or guid" unless $params{id} || $params{dbfile} || $params{guid};
+ croak "dbfile has to be of type SL::DB::File"
+ if defined $params{dbfile} && ref $params{dbfile} ne 'SL::DB::File';
+
+ my $dbfile;
+ my $file_version;
+ if (defined $params{guid}) {
+ $file_version = SL::DB::Manager::FileVersion->get_first(where => [guid => $params{guid}]);
+ die 'file version with guid not found: ' . $params{guid} unless $file_version;
+ $dbfile = $file_version->file;
+ if (defined $params{dbfile}) {
+ croak "dbfile doesn't match guid" if $dbfile->id != $params{dbfile}->id;
+ }
+ if (defined $params{id}) {
+ croak "id doesn't match guid" if $dbfile->id != $params{id};
+ }
+ } elsif (defined $params{dbfile}) {
+ $dbfile = $params{dbfile};
+ if (defined $params{id}) {
+ croak "id doesn't match dbfile id" if $dbfile->_id != $params{id};
+ }
+ } elsif (defined $params{id}) {
+ $dbfile = SL::DB::Manager::File->get_first(query => [id => $params{id}]);
+ die 'file with id not found: ' . $params{id} unless $dbfile;
+ }
+
+ my %object_params = (
+ db_file => $dbfile,
+ id => $dbfile->id,
+ loaded => 1,
+ );
+ $object_params{file_version} = $file_version if $file_version;
+ SL::File::Object->new(%object_params);