// ControlServer.java
package com.stock.pignon;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import org.json.JSONObject;
import fi.iki.elonen.NanoHTTPD;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Scanner;
/**
* Create a web server to remote management of app assets.
* Inherit what is needed to build a web server from NanoHTTPD
*/
public class ControlServer extends NanoHTTPD {
private final Context context;
// Assets folders
private final File storageRoot = Environment.getExternalStorageDirectory();
private final File baseDir = new File(storageRoot, Config.EXTERNAL_DIR_NAME);
private final File imagesDir = new File(baseDir, Config.IMAGES_SUBDIR_NAME);
public ControlServer(Context context, int port) {
super(port);
this.context = context;
}
/**
* Main method which manage requests
*/
@Override
public Response serve(IHTTPSession session) {
// Get requested address
String uri = session.getUri();
if (uri.startsWith("/img_view/")) {
return serveImage(uri.replace("/img_view/", ""));
}
if (session.getMethod() == Method.GET) {
switch (uri) {
// Soon deprecated with online editor
case "/download_input":
return downloadFile(Config.INPUT_JSON_NAME);
// Soon deprecated with online editor
case "/output_json":
return viewFile(Config.OUTPUT_JSON_NAME);
case "/download_output_json":
return downloadFile(Config.OUTPUT_JSON_NAME);
case "/output_csv":
return viewFile(Config.OUTPUT_CSV_NAME);
case "/download_output_csv":
return downloadFile(Config.OUTPUT_CSV_NAME);
case "/edit":
return newFixedLengthResponse(fillEditor());
case "/":
return newFixedLengthResponse(getHtml("index.html"));
}
}
if (session.getMethod() == Method.POST) {
switch (uri) {
// Soon deprecated with online editor
case "/upload_json":
return handleJsonUpload(session);
// Soon deprecated with online editor
case "/upload_images":
return handleImagesUpload(session);
case "/save_editor":
return handleSaveEditor(session);
}
}
return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "❌ Page non trouvée");
}
/**
* Read from asset and return html file
*/
private String getHtml(String file) {
try {
InputStream is = context.getAssets().open(file);
// Scanner html file from the beginning with "\\A"
Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\A");
String html = s.hasNext() ? s.next() : "";
is.close();
return html;
} catch (IOException e) {
return "
❌ Erreur de chargement de la page";
}
}
/**
* Read from asset and return html file
*/
private String fillEditor() {
// Get data from assets
DataLoader.loadData();
// Create an html string to fill the template
StringBuilder html = new StringBuilder();
// Save categories order
List orderList = new ArrayList<>();
// Globals
String globalId = "global";
Category globalCat = new Category("global", DataLoader.getGlobalItems());
html.append(renderCategorySection(globalId, globalCat));
orderList.add(globalId);
// Browser categories
int index = 0;
for (Category cat : DataLoader.getCategories()) {
// cat_0, cat_1...
String techId = "cat_" + index;
html.append(renderCategorySection(techId, cat));
orderList.add(techId);
index++;
}
String orderField = "";
return getHtml("editor.html").replace("{{GENERATED_CONTENT}}", html + orderField);
}
/**
* Generate HTML section for each category
*/
private String renderCategorySection(String techId, Category cat) {
String displayName = cat.getName();
List items = cat.getItems();
StringBuilder sb = new StringBuilder();
sb.append("
");
sb.append("
");
if ("global".equals(displayName)) {
sb.append("Articles globaux");
sb.append("");
techId = "global";
} else {
// Nom
sb.append("");
// Couleur de Fond (on utilise cat.getBgColor())
sb.append("
");
sb.append("
Fond
");
sb.append("");
sb.append("
");
// Couleur de Texte (on utilise cat.getTextColor())
sb.append("