+public errordomain FakeServerError {
+ FILE_DOES_NOT_EXIST
+}
+
+public class FakeServerCache : Object
+{
+ public string fname;
+ public uint8[] data;
+ public string content_type;
+ public int64 size;
+
+ public static Gee.HashMap<string,FakeServerCache> cache;
+
+ public static FakeServerCache factory(string fname, string scheme)
+ {
+ if (cache == null) {
+ cache = new Gee.HashMap<string,FakeServerCache>();
+ }
+
+ // print ("CACHE look for ==%s==\n", fname);
+ if (scheme == "resources") {
+ return new FakeServerCache.from_resource(fname);
+ }
+
+ if (cache.has_key(fname)) {
+ print ("CACHE got %s\n", fname);
+ return cache.get(fname);
+ }
+ print ("CACHE create %s\n", fname);
+
+ var el = new FakeServerCache(fname);
+
+ cache.set(fname, el);
+ return el;
+ }
+ // called onload to clear the temporary cached file..
+ public static void remove(string fname) {
+ if (cache == null) {
+ return;
+ }
+ if (!cache.has_key(fname)) {
+ return;
+ }
+
+
+ FakeServerCache v;
+
+ cache.unset(fname, out v);
+
+
+
+ }
+ public static void clear()
+ {
+ if (cache == null) {
+ return;
+ }
+ cache.clear();
+ }
+
+ public static FakeServerCache factory_with_data(string data) {
+ if (cache == null) {
+ cache = new Gee.HashMap<string,FakeServerCache>();
+ }
+ var el = new FakeServerCache.with_data(data);
+ print("CACHE - store %s\n", el.fname);
+ cache.set(el.fname, el);
+ return el;
+ }
+
+
+
+
+ public FakeServerCache.with_data( string data )
+ {
+ this.fname = "/" + GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5, data, data.length) + ".js";
+ this.data = data.data;
+ this.content_type = "text/javascript";
+ this.size= data.length;
+
+
+ }
+ public FakeServerCache.from_resource( string fname )
+ {
+
+
+
+ this.fname = fname;
+
+ var file = File.new_for_path ( BuilderApplication.configDirectory() + "/resources/" + fname);
+ if (!file.query_exists()) {
+ this.data = "".data;
+ this.content_type = "";
+ this.size = 0;
+ return;
+ }
+ try {
+ var info = file.query_info(
+ "standard::*",
+ FileQueryInfoFlags.NONE
+ );
+
+ this.content_type = info.get_content_type();
+ this.size = info.get_size();
+ uint8[] data;
+
+
+ GLib.FileUtils.get_data(file.get_path(), out data);
+ this.data = data;
+ } catch (Error e) {
+ this.data = "".data;
+ this.size = 0;
+ this.content_type = "";
+ return;
+ }
+
+
+
+ }
+ public FakeServerCache( string fname ) {
+
+ this.fname = fname;
+
+ var file = File.new_for_path ( GLib.Environment.get_home_dir() + "/gitlive" + fname);
+ if (!file.query_exists()) {
+ this.data = "".data;
+ this.content_type = "";
+ this.size = 0;
+ return;
+ }
+ try {
+ var info = file.query_info(
+ "standard::*",
+ FileQueryInfoFlags.NONE
+ );
+ this.content_type = info.get_content_type();
+ this.size = info.get_size();
+ uint8[] data;
+
+
+ GLib.FileUtils.get_data(file.get_path(), out data);
+ this.data = data;
+ } catch (Error e) {
+ this.data = "".data;
+ this.size = 0;
+ this.content_type = "";
+ return;
+ }
+
+
+
+ print("FakeServerCache :%s, %s (%s/%d)\n", fname ,
+ this.content_type, this.size.to_string(), this.data.length);
+
+
+ }
+
+
+ public void run(WebKit.URISchemeRequest request, Cancellable? cancellable)
+ {
+ var stream = new GLib.MemoryInputStream.from_data (this.data, GLib.free);
+ print("SEND %s\nwe", this.size.to_string());
+
+ request.finish(stream,
+ this.size,
+ this.content_type);
+
+
+
+ return;
+
+ }
+
+
+}