mirror of https://github.com/FongMi/TV.git
parent
337ad28dfe
commit
8450a2aa06
File diff suppressed because one or more lines are too long
@ -0,0 +1,103 @@ |
||||
package com.fongmi.android.tv.server.process; |
||||
|
||||
import com.fongmi.android.tv.server.Nano; |
||||
import com.github.catvod.utils.Path; |
||||
import com.google.gson.JsonArray; |
||||
import com.google.gson.JsonObject; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Arrays; |
||||
import java.util.Date; |
||||
import java.util.Locale; |
||||
import java.util.Map; |
||||
|
||||
import fi.iki.elonen.NanoHTTPD; |
||||
|
||||
public class Local implements Process { |
||||
|
||||
private final SimpleDateFormat format; |
||||
|
||||
public Local() { |
||||
this.format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.getDefault()); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isRequest(NanoHTTPD.IHTTPSession session, String path) { |
||||
return path.startsWith("/file") || path.startsWith("/upload") || path.startsWith("/newFolder") || path.startsWith("/delFolder") || path.startsWith("/delFile"); |
||||
} |
||||
|
||||
@Override |
||||
public NanoHTTPD.Response doResponse(NanoHTTPD.IHTTPSession session, String path, Map<String, String> files) { |
||||
if (path.startsWith("/file")) return getFile(path); |
||||
if (path.startsWith("/upload")) return upload(session.getParms(), files); |
||||
if (path.startsWith("/newFolder")) return newFolder(session.getParms()); |
||||
if (path.startsWith("/delFolder") || path.startsWith("/delFile")) return delFolder(session.getParms()); |
||||
return null; |
||||
} |
||||
|
||||
private NanoHTTPD.Response getFile(String url) { |
||||
try { |
||||
File file = Path.root(url.substring(6)); |
||||
if (file.isFile()) return Nano.newChunkedResponse(NanoHTTPD.Response.Status.OK, "application/octet-stream", new FileInputStream(file)); |
||||
else return Nano.success(listFiles(file)); |
||||
} catch (Exception e) { |
||||
return Nano.error(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
private NanoHTTPD.Response upload(Map<String, String> params, Map<String, String> files) { |
||||
String path = params.get("path"); |
||||
for (String k : files.keySet()) { |
||||
String fn = params.get(k); |
||||
File temp = new File(files.get(k)); |
||||
if (fn.toLowerCase().endsWith(".zip")) Path.unzip(temp, Path.root(path)); |
||||
else Path.copy(temp, Path.root(path, fn)); |
||||
} |
||||
return Nano.success(); |
||||
} |
||||
|
||||
private NanoHTTPD.Response newFolder(Map<String, String> params) { |
||||
String path = params.get("path"); |
||||
String name = params.get("name"); |
||||
Path.root(path, name).mkdirs(); |
||||
return Nano.success(); |
||||
} |
||||
|
||||
private NanoHTTPD.Response delFolder(Map<String, String> params) { |
||||
String path = params.get("path"); |
||||
Path.clear(Path.root(path)); |
||||
return Nano.success(); |
||||
} |
||||
|
||||
private String getParent(File root) { |
||||
return root.equals(Path.root()) ? "." : root.getParent().replace(Path.rootPath(), ""); |
||||
} |
||||
|
||||
private String listFiles(File root) { |
||||
File[] list = root.listFiles(); |
||||
String parent = getParent(root); |
||||
JsonObject info = new JsonObject(); |
||||
info.addProperty("parent", parent); |
||||
if (list == null || list.length == 0) { |
||||
info.add("files", new JsonArray()); |
||||
return info.toString(); |
||||
} |
||||
Arrays.sort(list, (o1, o2) -> { |
||||
if (o1.isDirectory() && o2.isFile()) return -1; |
||||
return o1.isFile() && o2.isDirectory() ? 1 : o1.getName().compareTo(o2.getName()); |
||||
}); |
||||
JsonArray files = new JsonArray(); |
||||
info.add("files", files); |
||||
for (File file : list) { |
||||
JsonObject obj = new JsonObject(); |
||||
obj.addProperty("name", file.getName()); |
||||
obj.addProperty("path", file.getAbsolutePath().replace(Path.rootPath(), "")); |
||||
obj.addProperty("time", format.format(new Date(file.lastModified()))); |
||||
obj.addProperty("dir", file.isDirectory() ? 1 : 0); |
||||
files.add(obj); |
||||
} |
||||
return info.toString(); |
||||
} |
||||
} |
||||
@ -1,10 +1,12 @@ |
||||
package com.fongmi.android.tv.server.process; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import fi.iki.elonen.NanoHTTPD; |
||||
|
||||
public interface RequestProcess { |
||||
public interface Process { |
||||
|
||||
boolean isRequest(NanoHTTPD.IHTTPSession session, String path); |
||||
|
||||
NanoHTTPD.Response doResponse(NanoHTTPD.IHTTPSession session, String path); |
||||
NanoHTTPD.Response doResponse(NanoHTTPD.IHTTPSession session, String path, Map<String, String> files); |
||||
} |
||||
@ -1,37 +0,0 @@ |
||||
package com.fongmi.android.tv.server.process; |
||||
|
||||
import com.fongmi.android.tv.App; |
||||
import com.fongmi.android.tv.server.Nano; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
|
||||
import fi.iki.elonen.NanoHTTPD; |
||||
|
||||
public class RawRequestProcess implements RequestProcess { |
||||
|
||||
private final String mimeType; |
||||
private final String path; |
||||
private final int resId; |
||||
|
||||
public RawRequestProcess(String path, int resId, String mimeType) { |
||||
this.path = path; |
||||
this.resId = resId; |
||||
this.mimeType = mimeType; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isRequest(NanoHTTPD.IHTTPSession session, String path) { |
||||
return session.getMethod() == NanoHTTPD.Method.GET && path.equalsIgnoreCase(this.path); |
||||
} |
||||
|
||||
@Override |
||||
public NanoHTTPD.Response doResponse(NanoHTTPD.IHTTPSession session, String path) { |
||||
try { |
||||
InputStream is = App.get().getResources().openRawResource(resId); |
||||
return Nano.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, mimeType + ";charset=utf-8", is, is.available()); |
||||
} catch (IOException e) { |
||||
return Nano.createErrorResponse(e.getMessage()); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue