package com.temporaryteam.noticeditor.io;

import java.io.*;
import java.net.URI;
import java.util.Deque;
import java.util.LinkedList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.json.JSONException;
import org.json.JSONObject;

public final class IOUtil {
	
	private static final String NEW_LINE = System.lineSeparator();

	public static String readContent(File file) throws IOException {
		final StringBuilder result = new StringBuilder();
		try (InputStream is = new FileInputStream(file);
				Reader isr = new InputStreamReader(is, "UTF-8");
				BufferedReader reader = new BufferedReader(isr)) {
			result.append(reader.readLine()).append(NEW_LINE);
		}
		return result.toString();
	}
	
	public static void writeContent(File file, String content) throws IOException {
		try (OutputStream os = new FileOutputStream(file);
				Writer writer = new OutputStreamWriter(os, "UTF-8")) {
			writer.write(content);
		}
	}
	
	public static void writeJson(File file, JSONObject json) throws IOException, JSONException {
		try (OutputStream os = new FileOutputStream(file);
				Writer writer = new OutputStreamWriter(os, "UTF-8")) {
			json.write(writer);
		}
	}
	
	public static void removeDirectory(File directory) {
		if (directory.isFile() || !directory.exists()) return;
		removeDirectoryHelper(directory);
	}
	
	private static void removeDirectoryHelper(File file) {
		if (file.isDirectory()) {
			for (File f : file.listFiles()) {
				removeDirectoryHelper(f);
			}
		}
		file.delete();
	}
	
	/** 
	 * Pack directory
	 */
	public static void pack(File directory, String toSave) throws IOException {
		final URI root = directory.toURI();
		Deque<File> queue = new LinkedList<File>();
		queue.push(directory);
		
		try (OutputStream out = new FileOutputStream(new File(toSave));
				ZipOutputStream zout = new ZipOutputStream(out)) {
			
			while(!queue.isEmpty()) {
				directory = queue.pop();
				for(File child : directory.listFiles()) {
					String name = root.relativize(child.toURI()).getPath();
					if (child.isDirectory()) {
						queue.push(child);
						name = name.endsWith("/") ? name : (name + "/");
						zout.putNextEntry(new ZipEntry(name));
					} else {
						zout.putNextEntry(new ZipEntry(name));
						try (InputStream in = new FileInputStream(child)) {
							byte[] buffer = new byte[1024];
							while(true) {
								int readCount = in.read(buffer);
								if (readCount < 0) break;
								zout.write(buffer, 0, readCount);
							}
						}
						zout.closeEntry();
					}
				}
			}
		}
	}
}

@@ -75,16 +75,44 @@
 et.URI;%0A
+import java.net.URLEncoder;%0A
 import j
@@ -127,16 +127,16 @@
 .Deque;%0A
-
 import j
@@ -315,16 +315,63 @@
 til %7B%0A%09%0A
+%09private static final int FILENAME_LIMIT = 60;%0A
 %09private
@@ -1595,16 +1595,585 @@
 );%0A%09%7D%0A%09%0A
+%09public static String sanitizeFilename(String name) %7B%0A%09%09if (name == null %7C%7C name.isEmpty()) return %22empty%22;%0A%09%09%0A%09%09// Convert non-ascii chars to char code%0A%09%09String newName = name;%0A%09%09try %7B%0A%09%09%09newName = URLEncoder.encode(newName, %22UTF-8%22);%0A%09%09%7D catch (UnsupportedEncodingException ex) %7B %7D%0A%09%09// Allow only english chars, numbers and some specific symbols%0A%09%09newName = newName.toLowerCase().replaceAll(%22%5B%5Ea-z0-9._%5C%5C(%5C%5C)%5D%22, %22_%22);%0A%09%09// Limit filename length%0A%09%09if (newName.length() %3E FILENAME_LIMIT) %7B%0A%09%09%09newName = newName.substring(0, FILENAME_LIMIT);%0A%09%09%7D%0A%09%09return newName;%0A%09%7D%0A%09%0A
 %09/** %0A%09 
@@ -2998,16 +2998,17 @@
 %09%09%09while
+ 
 (true) %7B
@@ -3189,13 +3189,15 @@
 %09%09%7D%0A%09%09%7D%0A
-
 %09%7D%0A
+%09%0A
 %7D%0A

Add method to sanitize filenames