Skip to content
This repository has been archived by the owner on May 23, 2019. It is now read-only.

Commit

Permalink
Update #168, debug and add dev mode, create controller, add routes, r…
Browse files Browse the repository at this point in the history
…emove concated flat file and keep only gz file, start remove old code, remove React lib js non min.
  • Loading branch information
hdsdi3g committed Dec 26, 2015
1 parent 49d6aa1 commit 6a57be2
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 19,706 deletions.
8 changes: 0 additions & 8 deletions app/controllers/Application.java
Expand Up @@ -39,7 +39,6 @@
import hd3gtv.mydmam.metadata.container.EntrySummary;
import hd3gtv.mydmam.module.MyDMAMModulesManager;
import hd3gtv.mydmam.web.Basket;
import hd3gtv.mydmam.web.JSSourceManager;
import hd3gtv.mydmam.web.PartialContent;
import hd3gtv.mydmam.web.search.SearchQuery;
import hd3gtv.mydmam.web.search.SearchRequest;
Expand Down Expand Up @@ -127,13 +126,6 @@ public static void index() {
} catch (Exception e) {
Loggers.Play.error("Can't get user basket", e);
}

try {
JSSourceManager.init(); // FIXME remove this !
} catch (Exception e) {
e.printStackTrace();
}

render(title, current_basket_content);
}

Expand Down
83 changes: 49 additions & 34 deletions app/controllers/AsyncJavascript.java
Expand Up @@ -16,20 +16,21 @@
*/
package controllers;

import java.io.FileNotFoundException;
import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import hd3gtv.mydmam.Loggers;
import hd3gtv.mydmam.web.AJSController;
import hd3gtv.mydmam.web.JSXTransformer;
import hd3gtv.mydmam.web.JSXTransformer.JSXItem;
import hd3gtv.mydmam.web.JsCompile;
import hd3gtv.mydmam.web.JSSourceManager;
import play.data.validation.Required;
import play.data.validation.Validation;
import play.mvc.Controller;
import play.mvc.With;
import play.utils.Utils;
import play.vfs.VirtualFile;

@With(Secure.class)
public class AsyncJavascript extends Controller {
Expand All @@ -48,38 +49,52 @@ public static void index(@Required String name, @Required String verb, @Required
renderJSON("{}");
}

public static void dynamicCompileJSX(@Required String ressource_name) {
try {
VirtualFile v_file = JsCompile.getTheFirstFromRelativePath(JSXItem.getRelativePathFromRessourceName(ressource_name), true, false);
if (v_file == null) {
throw new FileNotFoundException(ressource_name);
}
String etag = v_file.getRealFile().lastModified() + "--";
long last_modified = v_file.getRealFile().lastModified();

if (request.isModified(etag, last_modified) == false) {
response.setHeader("Etag", etag);
notModified();
public static void JavascriptRessource(@Required String name, Long suffix_date) {
if (Validation.hasErrors()) {
badRequest();
}

File ressource_file = JSSourceManager.getPhysicalFileFromRessourceName(name);
if (ressource_file == null) {
notFound();
}
long last_modified = ressource_file.lastModified();

String etag = last_modified + "--";

if (suffix_date != null) {
if (suffix_date > 0) {
response.setHeader("Cache-Control", "max-age=864000");
}

String jsx_compiled = JSXTransformer.getJSXContentFromURLList(v_file.getRealFile(), ressource_name, true, true);
/*if (request.headers.containsKey("accept-encoding")) {
if (request.headers.get("accept-encoding").values.contains("gzip")) {
response.setHeader("Content-Encoding", "gzip");
//TODO send GZIP
// renderBinary(is);
}
}*/
response.setHeader("Content-Length", jsx_compiled.length() + "");
response.setHeader("Content-Type", "text/javascript");
// response.setHeader("Cache-Control", "max-age=300"); //TODO set a long time by default, and add a generated URL ?date for each requests.
}

if (request.isModified(etag, last_modified) == false) {
response.setHeader("Etag", etag);
response.setHeader("Last-Modified", Utils.getHttpDateFormatter().format(new Date(last_modified)));
renderText(jsx_compiled);

} catch (Exception e) {
Loggers.Play.error("JSX Transformer Error", e);
notModified();
}

if (FilenameUtils.isExtension(ressource_file.getName(), "gz")) {
if (request.headers.containsKey("accept-encoding") == false) {
badRequest("// Your browser don't accept encoding files.");
}
if (request.headers.get("accept-encoding").value().indexOf("gzip") == -1) {
badRequest("// Your browser don't accept GZipped files.");
}
response.setHeader("Content-Encoding", "gzip");
}

response.setHeader("Content-Length", ressource_file.length() + "");
response.setHeader("Content-Type", "text/javascript");
response.setHeader("Etag", etag);
response.setHeader("Last-Modified", Utils.getHttpDateFormatter().format(new Date(last_modified)));

try {
FileUtils.copyFile(ressource_file, response.out);
} catch (IOException e) {
Loggers.Play.error("Can't response (send) js file: " + ressource_file, e);
notFound();
}
ok();
}

}
52 changes: 38 additions & 14 deletions app/hd3gtv/mydmam/web/JSProcessor.java
Expand Up @@ -110,15 +110,25 @@ public void warning(String arg0, String arg1, int arg2, String arg3, int arg4) {
private String input;
private String output;
private File filename;
private String module_name;
private String module_path;

public JSProcessor(File filename) throws NullPointerException, IOException {
public JSProcessor(File filename, String module_name, String module_path) throws NullPointerException, IOException {
this.module_name = module_name;
if (module_name == null) {
throw new NullPointerException("\"module_name\" can't to be null");
}
this.module_path = module_path;
if (module_path == null) {
throw new NullPointerException("\"module_path\" can't to be null");
}
this.filename = filename;
if (filename == null) {
throw new NullPointerException("\"filename\" can't to be null");
}
CopyMove.checkExistsCanRead(filename);
input = FileUtils.readFileToString(filename);
output = "";
output = input;
}

public void reduceJS() throws Exception {
Expand Down Expand Up @@ -205,39 +215,53 @@ public void wrapTransformationError(Exception e) {
int colon = error_message.indexOf(":", "Error: Parse Error: Line ".length());
int line_num = Integer.parseInt(error_message.substring("Error: Parse Error: Line ".length(), colon));
error_message = "Line " + line_num + ": " + error_message.substring(colon + 2);
if (line_num - 5 > -1) {
error_line = error_line + (line_num - 4) + " > " + escapeAll(lines[line_num - 5]) + "\\n";
}
if (line_num - 4 > -1) {
error_line = error_line + (line_num - 3) + " > " + escapeAll(lines[line_num - 4]) + "\\n";
}
if (line_num - 3 > -1) {
error_line = error_line + (line_num - 2) + " > " + escapeAll(lines[line_num - 3]) + "\\n";
}
if (line_num - 2 > -1) {
error_line = error_line + (line_num - 1) + " > " + escapeAll(lines[line_num - 2]) + "\\n";
error_line = error_line + (line_num - 1) + " > " + escapeAll(lines[line_num - 2]) + "\\n";
}
if ((line_num - 1 > 0) & (line_num - 1 < lines.length)) {
error_line = error_line + (line_num) + " > " + escapeAll(lines[line_num - 1]) + "\\n";
error_line = error_line + (line_num) + " >>> " + escapeAll(lines[line_num - 1]) + "\\n";
}
if (line_num < lines.length) {
error_line = error_line + (line_num + 1) + " > " + escapeAll(lines[line_num]);
error_line = error_line + (line_num + 1) + " > " + escapeAll(lines[line_num]) + "\\n";
}
if (line_num + 1 < lines.length) {
error_line = error_line + (line_num + 2) + " > " + escapeAll(lines[line_num + 1]) + "\\n";
}
if (line_num + 2 < lines.length) {
error_line = error_line + (line_num + 3) + " > " + escapeAll(lines[line_num + 2]) + "\\n";
}
if (line_num + 3 < lines.length) {
error_line = error_line + (line_num + 4) + " > " + escapeAll(lines[line_num + 3]);
}
} else {
Loggers.Play.error("Unknow transformation error", e);
}

if (error_message.indexOf("(file:") > -1) {
error_message = error_message.substring(0, error_message.lastIndexOf("(file:")).trim();
}

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
pw.println("// JSX ERROR");
pw.println("new function(){");
pw.println(" $(document).ready(function() {");
pw.println(" var message = {}");
pw.println(" message.from = \"" + filename + "\";");
pw.println(" message.from = \"" + module_name + " module: " + filename.getAbsolutePath().substring(module_path.length()) + "\";");
pw.println(" message.text = \"" + error_message + "\";");
pw.println(" message.line = \"" + error_line + "\";");
pw.println(" jsx_error_messages.push(message);");
pw.println(" });");
pw.println("}();");
pw.println();
pw.println("// SOURCE FILE");
for (int pos = 0; pos < lines.length; pos++) {
pw.print("// ");
pw.print(pos + 1);
pw.print(" ");
pw.println(lines[pos]);
}
pw.close();
output = new String(baos.toByteArray());
input = output;
Expand Down
4 changes: 4 additions & 0 deletions app/hd3gtv/mydmam/web/JSSourceDatabaseEntry.java
Expand Up @@ -146,6 +146,10 @@ private static String makeMD5(File source) throws IOException {
}

public String toString() {
return getRelativePath();
}

String getRelativePath() {
return relative_root_name + File.separator + relative_file_name;
}

Expand Down
103 changes: 62 additions & 41 deletions app/hd3gtv/mydmam/web/JSSourceManager.java
Expand Up @@ -16,9 +16,16 @@
*/
package hd3gtv.mydmam.web;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;

import hd3gtv.configuration.Configuration;
import hd3gtv.mydmam.Loggers;
import hd3gtv.tools.CopyMove;
import play.Play;
import play.vfs.VirtualFile;

Expand All @@ -28,14 +35,21 @@ private JSSourceManager() {
}

private static final ArrayList<JSSourceModule> js_modules;
private static final ArrayList<String> list_urls;
private static boolean js_dev_mode;

static {
js_modules = new ArrayList<JSSourceModule>(1);
list_urls = new ArrayList<String>(1);
js_dev_mode = Configuration.global.getValueBoolean("play", "js_dev_mode");
}

public static void init() throws Exception {
js_modules.clear();
if (isJsDevMode()) {
Loggers.Play.info("JS Source manager is in dev mode.");
}

js_modules.clear();
for (VirtualFile vfile : Play.roots) {
/**
* 1st pass : get only main, the first.
Expand All @@ -50,57 +64,64 @@ public static void init() throws Exception {
if (entry.getKey().startsWith("_")) {
continue;
}
// js_modules.add(new JSSourceModule(entry.getKey(), entry.getValue().getRealFile().getAbsoluteFile())); //XXX activate
}

for (int pos = 0; pos < js_modules.size(); pos++) {
js_modules.get(pos).processSources();
js_modules.add(new JSSourceModule(entry.getKey(), entry.getValue().getRealFile().getAbsoluteFile()));
}

/*Loggers.Play.info("Altered JS files: " + JSSourceDatabase.getAlteredFiles());
Loggers.Play.info("New JS files: " + JSSourceDatabase.getNewFiles());
JSSourceDatabase.saveAll();*/

/*
*
* public ArrayList<JSSourceDatabaseEntry> getAlteredFiles() {
ArrayList<JSSourceDatabaseEntry> result = new ArrayList<JSSourceDatabaseEntry>();
JSSourceDatabase db;
ArrayList<JSSourceDatabaseEntry> entries;
entries = checkAndClean();
if (entries.isEmpty() == false) {
result.put(module_name, entries);
refreshAllSources();
}

private static void refreshAllSources() throws Exception {
synchronized (list_urls) {
list_urls.clear();
for (int pos = 0; pos < js_modules.size(); pos++) {
js_modules.get(pos).processSources();
if (isJsDevMode()) {
list_urls.addAll(js_modules.get(pos).getTransformedFilesRelativeURLs());
} else {
list_urls.add(js_modules.get(pos).getConcatedFileRelativeURL());
}
}
}
return result;
}

public static File getPhysicalFileFromRessourceName(String ressource_name) {
String base_dir = JSSourceModule.BASE_REDUCED_DIRECTORY_JS;
if (isJsDevMode()) {
base_dir = JSSourceModule.BASE_TRANSFORMED_DIRECTORY_JS;
}

public static ArrayList<JSSourceDatabaseEntry> getNewFiles() {
ArrayList<JSSourceDatabaseEntry> result = new ArrayList<JSSourceDatabaseEntry>();
JSSourceDatabase db;
ArrayList<JSSourceDatabaseEntry> entries;
db = modules_databases.get(pos);
entries = db.newEntries();
if (entries.isEmpty() == false) {
result.put(db.module_name, entries);
VirtualFile ressource_file = VirtualFile.search(Play.roots, base_dir + File.separator + FilenameUtils.getName(ressource_name));
if (ressource_file == null) {
return null;
}
return result;
File result = ressource_file.getRealFile();
try {
CopyMove.checkExistsCanRead(result);
if (result.isFile() == false) {
throw new FileNotFoundException("File \"" + result + "\" exists, but is not a file.");
}

} catch (Exception e) {
Loggers.Play.warn("Invalid JS ressource: " + ressource_name, e);
return null;
}
*
* */

/*for (int pos = 0; pos < js_databases.size(); pos++) {
return result;
}

public static ArrayList<String> getURLs() {
if (isJsDevMode()) {
try {
js_databases.get(pos).save();
} catch (IOException e) {
Loggers.Play.error("Can't save entries");
refreshAllSources();
} catch (Exception e) {
Loggers.Play.warn("Can't refresh all JS source in dev mode", e);
}
}*/

}
return list_urls;
}

// TODO Controler Side

// TODO View side (link)
private static boolean isJsDevMode() {
return js_dev_mode;
}

// TODO add options in play page (switch dev/prod, force refresh)

Expand Down

0 comments on commit 6a57be2

Please sign in to comment.