--- loncom/interface/londocs.pm 2005/07/25 02:35:29 1.196 +++ loncom/interface/londocs.pm 2005/07/26 13:30:34 1.197 @@ -1,7 +1,7 @@ # The LearningOnline Network # Documents # -# $Id: londocs.pm,v 1.196 2005/07/25 02:35:29 raeburn Exp $ +# $Id: londocs.pm,v 1.197 2005/07/26 13:30:34 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -249,7 +249,6 @@ sub dumpcourse { # ------------------------------------------------------ Generate "export" button sub exportbutton { - return ''; return ''. ''. @@ -748,11 +747,13 @@ sub replicate_content { if ($copiedfile = Apache::File->new('>'.$destination)) { my $content; if ($caller eq 'resource') { - $content = &Apache::lonnet::getfile('/home/httpd/html/res/'.$url); + my $respath = $Apache::lonnet::perlvar{'lonDocRoot'}.'/res'; + my $filepath = &Apache::lonnet::filelocation($respath,$url); + $content = &Apache::lonnet::getfile($filepath); if ($content eq -1) { $$message = 'Could not copy file '.$filename; } else { - &extract_media($content,$count,$tempexport,$href,'resource'); + &extract_media($url,$cdom,$cnum,\$content,$count,$tempexport,$href,$message,'resource'); $repstatus = 'ok'; } } elsif ($caller eq 'uploaded' || $caller eq 'templateupload') { @@ -760,10 +761,10 @@ sub replicate_content { $repstatus = &Apache::lonnet::getuploaded('GET',$url,$cdom,$cnum,\$content,$rtncode); if ($repstatus eq 'ok') { if ($url =~ /\.html?$/i) { - &extract_media(\$content,$count,$tempexport,$href,'uploaded'); + &extract_media($url,$cdom,$cnum,\$content,$count,$tempexport,$href,$message,'uploaded'); } } else { - $$message = 'Could not render '.$url.' server message - '.$rtncode; + $$message = 'Could not render '.$url.' server message - '.$rtncode."
\n"; } } elsif ($caller eq 'noedit') { # Need to render the resource without the LON-CAPA Internal header and the Post discussion footer, and then set $content equal to this. @@ -775,10 +776,10 @@ sub replicate_content { } close($copiedfile); } else { - $$message = 'Could not open destination file for '.$filename."\n"; + $$message = 'Could not open destination file for '.$filename."
\n"; } } else { - $$message = 'Could not determine name of file for '.$symb."\n"; + $$message = 'Could not determine name of file for '.$symb."
\n"; } if ($repstatus eq 'ok') { $content_name = $count.'/'.$filename; @@ -787,11 +788,66 @@ sub replicate_content { } sub extract_media { - my ($content,$count,$tempexport,$href,$caller) = @_; -# @$href will contain path to any embedded resources in the content. -# For LON-CAPA problems this would be images. applets etc. -# For uploaded HTML files this would be images etc. -# paths will be in the form $count/res/$file, and urls in the $content will be rewritten with the new paths. + my ($url,$cdom,$cnum,$content,$count,$tempexport,$href,$message,$caller) = @_; + my %allfiles = (); + my %codebase = (); + $url =~ s#([^/]+)$##; + &Apache::lonnet::extract_embedded_items(undef,undef,\%allfiles,\%codebase,$content); + foreach my $embed_file (keys(%allfiles)) { + my $filename; + if ($embed_file =~ m#([^/]+)$#) { + $filename = $1; + } else { + $filename = $embed_file; + } + my $newname = 'res/'.$filename; + my ($rtncode,$embed_content,$repstatus); + my $embed_url; + if ($embed_file =~ m-^/-) { + $embed_url = $embed_file; # points to absolute path + } else { + if ($embed_file =~ m-https?://-) { + next; # points to url + } else { + $embed_url = $url.$embed_file; # points to relative path + } + } + if ($caller eq 'resource') { + my $respath = $Apache::lonnet::perlvar{'lonDocRoot'}.'/res'; + my $embed_path = &Apache::lonnet::filelocation($respath,$embed_url); + $embed_content = &Apache::lonnet::getfile($embed_path); + unless ($embed_content eq -1) { + $repstatus = 'ok'; + } + } elsif ($caller eq 'uploaded') { + + $repstatus = &Apache::lonnet::getuploaded('GET',$embed_url,$cdom,$cnum,\$embed_content,$rtncode); + } + if ($repstatus eq 'ok') { + my $destination = $tempexport.'/resources/'.$count.'/res'; + if (!-e "$destination") { + mkdir($destination,0755); + } + $destination .= '/'.$filename; + my $copiedfile; + if ($copiedfile = Apache::File->new('>'.$destination)) { + print $copiedfile $embed_content; + push @{$href}, .'resources/'.$count.'/res/'.$filename; + my $attrib_regexp = ''; + if (@{$allfiles{$embed_file}} > 1) { + $attrib_regexp = join('|',@{$allfiles{$embed_file}}); + } else { + $attrib_regexp = $allfiles{$embed_file}[0]; + } + $$content =~ s#($attrib_regexp\s*=\s*['"]?)\Q$embed_file\E(['"]?)#$1$newname$2#gi; + if ($caller eq 'resource' && $url =~ /\.(problem|library)$/) { + $$content =~ s#\Q$embed_file\E#$newname#gi; + } + } + } else { + $$message .= 'replication of embedded file - '.$embed_file.' in '.$url.' failed, reason -'.$rtncode."
\n"; + } + } return; }