From 60a6240c9278c7d3724818da8084f4c395f386e2 Mon Sep 17 00:00:00 2001 From: Volodymyr Vakulenko Date: Thu, 13 Jul 2017 15:51:15 +0300 Subject: [PATCH] Add ability to upload image handouts (#254) * Add ability to upload image handouts * Extend handouts allowed file types list * Refactor uploading file extension check, cover it with unittests --- .../transcript-manual-upload-spec.js | 26 ++++++++++++++++--- .../studio-edit/transcripts-manual-upload.js | 25 +++++++++++++++++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/video_xblock/static/js/spec/studio-edit/transcript-manual-upload-spec.js b/video_xblock/static/js/spec/studio-edit/transcript-manual-upload-spec.js index 74897373..835cc04c 100644 --- a/video_xblock/static/js/spec/studio-edit/transcript-manual-upload-spec.js +++ b/video_xblock/static/js/spec/studio-edit/transcript-manual-upload-spec.js @@ -1,4 +1,4 @@ -/* global parseRelativeTime validateTranscripts*/ +/* global parseRelativeTime validateTranscripts getAllowedFileExtensions*/ /** * Tests for transcripts manual upload */ @@ -21,7 +21,8 @@ describe('Transcripts manual upload', function() { expect(parseRelativeTime(value)).toBe(tests[value]); }); }); - it('return getTranscriptUrl', function() { + + it('returns getTranscriptUrl', function() { var transcriptsArray = [ { lang: 'en', @@ -37,7 +38,7 @@ describe('Transcripts manual upload', function() { expect(getTranscriptUrl(transcriptsArray)).toBe(''); }); - it('return validateTranscripts', function() { + it('returns validateTranscripts', function() { var $testTranscriptsBlock; $('body').append('
    ' + '
  1. ' + @@ -53,3 +54,22 @@ describe('Transcripts manual upload', function() { expect(validateTranscripts($testTranscriptsBlock)).toBeTruthy(); }); }); + +describe('Correct file extensions are returned when', function() { + 'use strict'; + it('file is uploading in "transcripts" context', function() { + expect(getAllowedFileExtensions('transcripts')).toEqual('.srt, .vtt'); + }); + + it('file is uploading in other then "transcripts" context', function() { + var handoutsAllowedFileTypes = ( + '.gif, .ico, .jpg, .jpeg, .png, .tif, .tiff, .bmp, .svg, ' + // images + '.pdf, .txt, .rtf, .csv, ' + // documents + '.doc, .docx, .xls, .xlsx, .ppt, .pptx, .pub, ' + // MSOffice + '.odt, .ods, .odp, ' + // openOffice + '.zip, .7z, .gzip, .tar ' + // archives + '.html, .xml, .js, .sjson' // other + ); + expect(getAllowedFileExtensions('somethings_else_or_null')).toEqual(handoutsAllowedFileTypes); + }); +}); diff --git a/video_xblock/static/js/studio-edit/transcripts-manual-upload.js b/video_xblock/static/js/studio-edit/transcripts-manual-upload.js index 0038fbd7..d2737631 100644 --- a/video_xblock/static/js/studio-edit/transcripts-manual-upload.js +++ b/video_xblock/static/js/studio-edit/transcripts-manual-upload.js @@ -234,16 +234,39 @@ function createTranscriptBlock(langCode, langLabel, transcriptsValue, downloadTr $('.add-transcript').removeClass('is-disabled'); } +/** + * Return string with allowed for uploading file extensions by given uploading context ('transcripts'/'handouts'). + */ +function getAllowedFileExtensions(uploadingContext) { + 'use strict'; + var transcriptsAllowedFileExtensions = '.srt, .vtt'; + var handoutsAllowedFileTypes = ( + '.gif, .ico, .jpg, .jpeg, .png, .tif, .tiff, .bmp, .svg, ' + // images + '.pdf, .txt, .rtf, .csv, ' + // documents + '.doc, .docx, .xls, .xlsx, .ppt, .pptx, .pub, ' + // MSOffice + '.odt, .ods, .odp, ' + // openOffice + '.zip, .7z, .gzip, .tar ' + // archives + '.html, .xml, .js, .sjson' // other + ); + switch (uploadingContext) { + case 'transcripts': + return transcriptsAllowedFileExtensions; + default: + return handoutsAllowedFileTypes; + } +} + /** * Assign transcript's data to file uploader's attributes. */ function clickUploader(event, $fileUploader) { 'use strict'; + var $buttonBlock = $(event.currentTarget); var indexOfParentLi = $('.language-transcript-selector').children().index($buttonBlock.closest('li')); var langCode = $buttonBlock.attr('data-lang-code'); var langLabel = $buttonBlock.attr('data-lang-label'); - var fieldNameDetails = $buttonBlock.attr('data-change-field-name') === 'transcripts' ? '.srt, .vtt' : ''; + var fieldNameDetails = getAllowedFileExtensions($buttonBlock.attr('data-change-field-name')); var fieldName = $buttonBlock.attr('data-change-field-name'); var dataLiIndex = $buttonBlock.attr('data-change-field-name') === 'transcripts' ? indexOfParentLi : ''; event.preventDefault();