Skip to content

Rewrite the WeBWorK::Upload module. #2693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions lib/WeBWorK.pm
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,14 @@ async sub dispatch ($c) {

my @uploads = @{ $c->req->uploads };

foreach my $u (@uploads) {
# Make sure it's a "real" upload.
next unless $u->filename;

for my $u (@uploads) {
# Store the upload.
my $upload = WeBWorK::Upload->store($u, dir => $ce->{webworkDirs}{uploadCache});
my $upload = WeBWorK::Upload->store($u, $ce->{webworkDirs}{uploadCache});

# Store the upload ID and hash in the file upload field.
my $id = $upload->id;
my $hash = $upload->hash;
$c->param($u->name => "$id $hash");
# Store the upload temporary file location and hash in the file upload field.
my $tmpFile = $upload->tmpFile;
my $hash = $upload->hash;
$c->param($u->name => "$tmpFile $hash");
}

# Create these out here. They should fail if they don't have the right information.
Expand Down
4 changes: 2 additions & 2 deletions lib/WeBWorK/ContentGenerator/Feedback.pm
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ sub initialize ($c) {
my $fileIDhash = $c->param('attachment');
if ($fileIDhash) {
my $attachment =
WeBWorK::Upload->retrieve(split(/\s+/, $fileIDhash), dir => $ce->{webworkDirs}{uploadCache});
WeBWorK::Upload->retrieve(split(/\s+/, $fileIDhash), $ce->{webworkDirs}{uploadCache});

# Get the filename and read its contents.
my $filename = $attachment->filename;
Expand All @@ -184,7 +184,7 @@ sub initialize ($c) {
local $/;
$contents = <$fh>;
};
close $fh;
$fh->close;
$attachment->dispose;

# Check to see that this is an allowed filetype.
Expand Down
22 changes: 12 additions & 10 deletions lib/WeBWorK/ContentGenerator/Instructor/FileManager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,7 @@ sub Upload ($c) {
return $c->Refresh;
}

my ($id, $hash) = split(/\s+/, $fileIDhash);
my $upload = WeBWorK::Upload->retrieve($id, $hash, dir => $c->{ce}{webworkDirs}{uploadCache});
my $upload = WeBWorK::Upload->retrieve(split(/\s+/, $fileIDhash), $c->{ce}{webworkDirs}{uploadCache});

my $name = $upload->filename;
my $invalidUploadFilenameMsg = $c->checkName($name);
Expand Down Expand Up @@ -794,21 +793,24 @@ sub Upload ($c) {
if ($type ne 'Binary') {
my $fh = $upload->fileHandle;
my @lines = <$fh>;
$fh->close;
$data = join('', @lines);
if ($type eq 'Automatic') { $type = isText($data) ? 'Text' : 'Binary' }
}
if ($type eq 'Text') {
$upload->dispose;
$data =~ s/\r\n?/\n/g;

my $backup_data = $data;
my $success = utf8::decode($data); # try to decode as utf8
unless ($success) {
warn "Trying to convert file $file from latin1? to UTF-8";
utf8::upgrade($backup_data); # try to convert data from latin1 to utf8.
$data = $backup_data;
}

if (open(my $UPLOAD, '>:encoding(UTF-8)', $file)) {
my $backup_data = $data;
my $success = utf8::decode($data); # try to decode as utf8
unless ($success) {
warn "Trying to convert file $file from latin1? to UTF-8";
utf8::upgrade($backup_data); # try to convert data from latin1 to utf8.
$data = $backup_data;
}
print $UPLOAD $data; # print massaged data to file.
print $UPLOAD $data;
close $UPLOAD;
} else {
$c->addbadmessage($c->maketext(q{Can't create file "[_1]": [_2]}, $name, $!));
Expand Down
Loading