Skip to content

Commit 9deb205

Browse files
committed
Added ModuleJoke, Added Filesize to FileServer
1 parent 4903006 commit 9deb205

File tree

6 files changed

+94
-22
lines changed

6 files changed

+94
-22
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# TODOOOOOO add joke comamnd https://sv443.net/jokeapi/v2/
2-
3-
4-
51
# ConsoleTools
62
###### by Command Jo
73
<br><br>
@@ -30,3 +26,4 @@ TO BE ADDED
3026
Json - prints beautified json
3127
Fileshare - shares files to a server
3228
Rename - renames a file
29+
Joke - prints a joke

src/main/java/de/jo/modules/impl/files/ModuleList.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public void run(String... args) throws Exception {
2929
if(s.length() > longestLength) longestLength=s.length();
3030
}
3131

32-
System.out.println(ConsoleColors.YELLOW_BRIGHT+"L___ "+ConsoleColors.YELLOW+dir.getCanonicalPath()+Strings.repeat((longestLength-dir.getCanonicalPath().length())+5, " ")+ConsoleColors.YELLOW_BRIGHT+" | "+ConsoleColors.RED_BRIGHT+sizeShortened(size(dir)));
32+
System.out.println(ConsoleColors.YELLOW_BRIGHT+"L___ "+ConsoleColors.YELLOW+dir.getCanonicalPath()+Strings.repeat((longestLength-dir.getCanonicalPath().length())+5, " ")+ConsoleColors.YELLOW_BRIGHT+" | "+ConsoleColors.RED_BRIGHT+Files.sizeShortened(size(dir)));
3333

3434
for(File file : Objects.requireNonNull(dir.listFiles())) {
35-
String filesize = sizeShortened((int) file.length());
35+
String filesize = Files.sizeShortened((int) file.length());
3636
int spaces = longestLength-file.getName().length()+5;
3737
System.out.println(ConsoleColors.YELLOW_BRIGHT+" L___ "+ConsoleColors.YELLOW+file.getName()+Strings.repeat(spaces, " ")+ConsoleColors.YELLOW_BRIGHT+"| "+ConsoleColors.RED_BRIGHT+filesize);
3838
}
@@ -49,18 +49,4 @@ public int size(File dir) {
4949
return size;
5050
}
5151

52-
public String sizeShortened(int sizeInBytes) {
53-
if(sizeInBytes < 1000) {
54-
return (sizeInBytes)+"b";
55-
}
56-
if(sizeInBytes < 1000000) {
57-
return (sizeInBytes/1000F)+"Kb";
58-
}
59-
if(sizeInBytes < 1000000000) {
60-
return (sizeInBytes/1000000F)+"Mb";
61-
}else{
62-
return (sizeInBytes/1000000000F)+"Gb";
63-
}
64-
}
65-
6652
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package de.jo.modules.impl.url;
2+
3+
import com.google.gson.*;
4+
import de.jo.modules.Module;
5+
import de.jo.modules.ModuleInfo;
6+
import de.jo.util.ConsoleColors;
7+
import de.jo.util.Files;
8+
import de.jo.util.Strings;
9+
10+
import java.io.InputStream;
11+
import java.net.URL;
12+
import java.util.List;
13+
14+
/**
15+
* @author Johannes Hans 29.10.2023
16+
* @Project ConsoleTools
17+
*/
18+
@ModuleInfo(name = "joke", description = "Prints a joke", syntax = "<Any|Programming,Misc,Dark,Pun,Spooky,Christmas(Category)> <fr|cs|de|en|es|pt(Language)>")
19+
public class ModuleJoke implements Module {
20+
21+
public static final String API_URL = "https://v2.jokeapi.dev/joke/";
22+
23+
@Override
24+
public void run(String... args) throws Exception {
25+
if(args.length == 2) {
26+
String category = args[0];
27+
String lang = args[1];
28+
29+
String urlString = API_URL+""+category+"?lang="+lang;
30+
URL url = new URL(urlString);
31+
InputStream is = url.openStream();
32+
if(is == null) {
33+
Strings.error("URL: \""+urlString+"\" not found");
34+
Strings.moduleError(this, 34);
35+
return;
36+
}
37+
List<String> lines = Files.lines(is);
38+
String response = Strings.lines(lines);
39+
JsonObject el = JsonParser.parseString(response).getAsJsonObject();
40+
if(!el.get("error").getAsBoolean()) {
41+
if(el.get("joke") != null) {
42+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+el.get("joke").getAsString());
43+
}else {
44+
new Thread(new Runnable() {
45+
@Override
46+
public void run() {
47+
try {
48+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+el.get("setup").getAsString());
49+
Thread.sleep(200);
50+
System.out.println(ConsoleColors.YELLOW+".");
51+
Thread.sleep(200);
52+
System.out.println(ConsoleColors.YELLOW+"..");
53+
Thread.sleep(200);
54+
System.out.println(ConsoleColors.YELLOW+"...");
55+
Thread.sleep(200);
56+
System.out.println(ConsoleColors.YELLOW+"....");
57+
Thread.sleep(200);
58+
System.out.println(ConsoleColors.YELLOW+"> "+ConsoleColors.YELLOW_BRIGHT+el.get("delivery").getAsString());
59+
} catch(Exception ex) {}
60+
}
61+
}).start();
62+
}
63+
}else{
64+
Strings.error("No joke found");
65+
}
66+
67+
}else{
68+
Strings.error("Invalid usage");
69+
}
70+
}
71+
}

src/main/java/de/jo/util/Files.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,18 @@ public static boolean isValidFileName(String name) {
8080
return true;
8181
}
8282

83+
public static String sizeShortened(int sizeInBytes) {
84+
if(sizeInBytes < 1000) {
85+
return (sizeInBytes)+"b";
86+
}
87+
if(sizeInBytes < 1000000) {
88+
return (sizeInBytes/1000F)+"Kb";
89+
}
90+
if(sizeInBytes < 1000000000) {
91+
return (sizeInBytes/1000000F)+"Mb";
92+
}else{
93+
return (sizeInBytes/1000000000F)+"Gb";
94+
}
95+
}
96+
8397
}

src/main/java/de/jo/web/FileServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void addDirectory(File file) {
6868
**/
6969
private static final String FILE_TEMPLATE = "<li>\n" +
7070
" <div class=\"filename\">%FILE_NAME%</div>\n" +
71-
" <button class=\"filelink\"><a href=\"%FILE_NAME%\">Download</a></button>\n" +
71+
" <button class=\"filelink\"><a href=\"%FILE_NAME%\">Download<br><p>Size: %FILE_SIZE%</p></a></button>\n" +
7272
" <div class=\"filehash\">sha256: <button>%FILE_HASH%</button></div>\n" +
7373
" </li>";
7474

@@ -98,7 +98,7 @@ public void handle(HttpExchange ex) throws IOException {
9898
for(File file : files) {
9999
addFileEntry(file);
100100
String hash = Strings.sha256(Strings.lines(de.jo.util.Files.lines(file)));
101-
filesList.append(FILE_TEMPLATE.replace("%FILE_NAME%", file.getName()).replace("%FILE_HASH%", hash)).append("<br>");
101+
filesList.append(FILE_TEMPLATE.replace("%FILE_NAME%", file.getName()).replace("%FILE_SIZE%", ""+ de.jo.util.Files.sizeShortened((int) file.length())).replace("%FILE_HASH%", hash)).append("<br>");
102102
}
103103
html = html.replace("%FILES%", filesList);
104104

src/main/resources/style.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ h1 {
1919
text-transform: uppercase;
2020
}
2121

22+
p {
23+
font-size: 1.5vh;
24+
}
25+
2226
#description {
2327
background-color: #393939;
2428
font-size: 2vw;

0 commit comments

Comments
 (0)