--- loncom/homework/daxesave.pm 2016/03/18 18:21:01 1.4 +++ loncom/homework/daxesave.pm 2023/08/23 20:43:34 1.6 @@ -1,7 +1,7 @@ # The LearningOnline Network # Convert and save a problem from Daxe. # -# $Id: daxesave.pm,v 1.4 2016/03/18 18:21:01 damieng Exp $ +# $Id: daxesave.pm,v 1.6 2023/08/23 20:43:34 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -28,8 +28,9 @@ ### package Apache::daxesave; +use strict; -use Apache::Constants; +use Apache::Constants qw(:common); use Apache::lonnet; use Try::Tiny; use File::Copy; @@ -82,22 +83,30 @@ sub handler { my $contents = $env{'form.file'}; - try { - $contents = &Apache::xml_to_loncapa::convert_file($contents); - } catch { - $request->print("error\nconvert failed for $path: $_"); - return OK; - }; + my $mode; + if ($path =~ /\.(task|problem|exam|quiz|assess|survey|library|xml|html|htm|xhtml|xhtm)$/) { + try { + $contents = &Apache::xml_to_loncapa::convert_file($contents); + } catch { + $request->print("error\nconvert failed for $path: $_"); + return OK; + }; + $mode = '>:encoding(UTF-8)'; + } else { + $mode = '>'; + } my $filebak = $newpath.".bak"; if (-e $newpath) { copy($newpath, $filebak); # errors ignored } - open my $out, '>:encoding(UTF-8)', $newpath; - print $out $contents; - close $out; - - $request->print("ok\n"); + if (open(my $out, $mode, $newpath)) { + print $out $contents; + close $out; + $request->print("ok\n"); + } else { + $request->print("error\nFailed to open file to save $path"); + } return OK; }