Skip to content

Commit e11c145

Browse files
committed
Check for binary files in diff editor
1 parent fbabd8a commit e11c145

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

webapp/src/Controller/Jury/SubmissionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ public function sourceAction(
906906
foreach ($oldFiles as $f) {
907907
$submitId = $f->getSubmission()->getSubmitid();
908908
$otherFiles[$submitId] ??= [];
909-
$otherFiles[$submitId][] = $f;
909+
$otherFiles[$submitId][$f->getFilename()] = $f;
910910
}
911911

912912
return $this->render('jury/submission_source.html.twig', [

webapp/src/Twig/TwigExtension.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -941,15 +941,16 @@ public function getMonacoModel(SubmissionFile $file): string
941941
}
942942
$this->renderedSources[$file->getSubmitfileid()] = true;
943943

944+
$source = mb_check_encoding($file->getSourcecode(), 'UTF-8') ? $file->getSourcecode() : "Could not display binary file";
944945
return sprintf(
945946
<<<JS
946947
monaco.editor.createModel(
947-
"%s",
948+
%s,
948949
undefined,
949950
monaco.Uri.parse("diff/%d/%s")
950951
);
951952
JS,
952-
$this->twig->getRuntime(EscaperRuntime::class)->escape($file->getSourcecode(), 'js'),
953+
$this->serializer->serialize($source, 'json'),
953954
$file->getSubmitfileid(),
954955
$file->getFilename(),
955956
);
@@ -968,7 +969,7 @@ public function showDiff(string $editorId, string $diffId, SubmissionFile $newFi
968969
const models = %s;
969970
require(['vs/editor/editor.main'], () => {
970971
const modifiedModel = %s;
971-
initDiffEditorTab(editorId, diffId, rank, models, modifiedModel)
972+
initDiffEditorTab(editorId, diffId, rank, models, modifiedModel);
972973
});
973974
});
974975
</script>
@@ -977,11 +978,12 @@ public function showDiff(string $editorId, string $diffId, SubmissionFile $newFi
977978
$others = [];
978979
foreach ($otherFiles as $submissionId => $files) {
979980
foreach ($files as $f) {
980-
if ($f->getFilename() == $newFile->getFilename()) {
981+
// TODO: this renames the old file to multiple files, need more data in `showDiff`.
982+
if (($newFile->getRank() === 1 && count($files) === 1) || ($f->getFilename() == $newFile->getFilename())) {
981983
// TODO: add `tag` containing `previous` / `original`
982984
$others[$submissionId] = [
983985
'filename' => $f->getFilename(),
984-
'source' => $f->getSourcecode(),
986+
'source' => mb_check_encoding($f->getSourcecode(), 'UTF-8') ? $f->getSourcecode() : "Could not display binary file",
985987
];
986988
}
987989
}

0 commit comments

Comments
 (0)