Encode-decode only image files
authorEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Thu, 3 Sep 2015 15:48:47 +0000 (18:48 +0300)
committerEkaterina Tuzova <Ekaterina.Tuzova@jetbrains.com>
Thu, 3 Sep 2015 16:03:09 +0000 (19:03 +0300)
python/educational/interactive-learning/src/com/jetbrains/edu/learning/courseGeneration/StudyProjectGenerator.java
python/educational/src/com/jetbrains/edu/EduUtils.java
python/educational/src/com/jetbrains/edu/stepic/EduStepicConnector.java

index 40d29b68a2897e8ca35df5ef6a3fa74f6af6c7b4..9f4e73aebc3c356605dea91926f4a342faea13c9 100644 (file)
@@ -23,6 +23,7 @@ import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiManager;
 import com.intellij.util.containers.ContainerUtil;
 import com.jetbrains.edu.EduNames;
+import com.jetbrains.edu.EduUtils;
 import com.jetbrains.edu.courseFormat.Course;
 import com.jetbrains.edu.courseFormat.Lesson;
 import com.jetbrains.edu.courseFormat.Task;
@@ -179,7 +180,12 @@ public class StudyProjectGenerator {
       final File file = new File(courseDirectory, name);
       FileUtil.createIfDoesntExist(file);
       try {
-        FileUtil.writeToFile(file, Base64.decodeBase64(text));
+        if (EduUtils.isImage(name)) {
+          FileUtil.writeToFile(file, Base64.decodeBase64(text));
+        }
+        else {
+          FileUtil.writeToFile(file, text);
+        }
       }
       catch (IOException e) {
         LOG.error("ERROR copying file " + name);
@@ -206,7 +212,13 @@ public class StudyProjectGenerator {
       FileUtil.createIfDoesntExist(file);
 
       try {
-        FileUtil.writeToFile(file, Base64.decodeBase64(taskFile.text));
+        if (EduUtils.isImage(taskFile.name)) {
+          FileUtil.writeToFile(file, Base64.decodeBase64(taskFile.text));
+        }
+        else {
+          FileUtil.writeToFile(file, taskFile.text);
+        }
+
       }
       catch (IOException e) {
         LOG.error("ERROR copying file " + name);
index bb472b22c444cf28cf1f5d2bd90a79a06d1e5e25..9f5738cbe162bdb34273546861a9487c8d1840f2 100644 (file)
@@ -23,9 +23,11 @@ import com.jetbrains.edu.courseFormat.Task;
 import com.jetbrains.edu.courseFormat.TaskFile;
 import com.jetbrains.edu.oldCourseFormat.OldCourse;
 import com.jetbrains.edu.oldCourseFormat.TaskWindow;
+import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import javax.imageio.ImageIO;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
@@ -331,4 +333,15 @@ public class EduUtils {
     course.initCourse(false);
     return course;
   }
+
+  public static boolean isImage(String fileName) {
+    final String[] readerFormatNames = ImageIO.getReaderFormatNames();
+    for (@NonNls String format : readerFormatNames) {
+      final String ext = format.toLowerCase();
+      if (fileName.endsWith(ext)) {
+        return true;
+      }
+    }
+    return false;
+  }
 }
index b278776b52ec38bafda6bd6d840ccc4cdbc62b06..74721f299fd485ff9963009be0cf150bb5c7dc95 100644 (file)
@@ -442,7 +442,12 @@ public class EduStepicConnector {
       for (VirtualFile file : files) {
         try {
           if (file != null) {
-            task.addTestsTexts(file.getName(), Base64.encodeBase64URLSafeString(FileUtil.loadBytes(file.getInputStream())));
+            if (EduUtils.isImage(file.getName())) {
+              task.addTestsTexts(file.getName(), Base64.encodeBase64URLSafeString(FileUtil.loadBytes(file.getInputStream())));
+            }
+            else {
+              task.addTestsTexts(file.getName(), FileUtil.loadTextAndClose(file.getInputStream()));
+            }
           }
         }
         catch (IOException e) {
@@ -617,7 +622,12 @@ public class EduStepicConnector {
         final VirtualFile file = taskDirectory.findChild(taskFile.name);
         try {
           if (file != null) {
-            taskFile.text = Base64.encodeBase64URLSafeString(FileUtil.loadBytes(file.getInputStream()));
+            if (EduUtils.isImage(taskFile.name)) {
+              taskFile.text = Base64.encodeBase64URLSafeString(FileUtil.loadBytes(file.getInputStream()));
+            }
+            else {
+              taskFile.text = FileUtil.loadTextAndClose(file.getInputStream());
+            }
           }
         }
         catch (IOException e) {