53 lines
1.4 KiB
Java
53 lines
1.4 KiB
Java
package tech.nevets.ngxinstaller;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
public class TextStorage {
|
|
private final String id;
|
|
private final String text;
|
|
private final long expiration;
|
|
|
|
private static final List<TextStorage> DB = new ArrayList<>();
|
|
|
|
private TextStorage(String id, String text, long expiration) {
|
|
this.id = id;
|
|
this.text = text;
|
|
this.expiration = expiration;
|
|
}
|
|
|
|
public String getId() {
|
|
return this.id;
|
|
}
|
|
|
|
public static TextStorage getPaste(String id) {
|
|
for (TextStorage ts : DB) {
|
|
if (ts.id.equals(id)) {
|
|
return ts;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static TextStorage createPaste(String text) {
|
|
TextStorage ts = new TextStorage(generateId(), text, System.currentTimeMillis() + 3600000);
|
|
DB.add(ts);
|
|
return ts;
|
|
}
|
|
|
|
private static String generateId() {
|
|
Random rand = ThreadLocalRandom.current();
|
|
UUID uuid = UUID.randomUUID();
|
|
String strUuid = uuid.toString().replace("-", "");
|
|
int randInt = rand.nextInt(strUuid.length() - 4);
|
|
return strUuid.substring(randInt, randInt + 4);
|
|
}
|
|
|
|
public static void garbageCollect() {
|
|
DB.removeIf(ts -> ts.expiration < System.currentTimeMillis());
|
|
}
|
|
}
|