Format error tip for banning

pull/137/head
johnniang 2019-03-29 15:08:38 +08:00
parent fdc294b335
commit fd59393634
3 changed files with 92 additions and 24 deletions

View File

@ -95,12 +95,11 @@ public class UserServiceImpl extends AbstractCrudService<User, Integer> implemen
// Check expiration // Check expiration
if (user.getExpireTime() != null && user.getExpireTime().after(now)) { if (user.getExpireTime() != null && user.getExpireTime().after(now)) {
long seconds = TimeUnit.MINUTES.toSeconds(user.getExpireTime().getTime() - now.getTime()); long seconds = TimeUnit.MILLISECONDS.toSeconds(user.getExpireTime().getTime() - now.getTime());
// If expired // If expired
throw new BadRequestException("You have been temporarily disabledplease try again " + seconds + " second(s) later").setErrorData(seconds); throw new BadRequestException("You have been temporarily disabledplease try again " + HaloUtils.timeFormat(seconds) + " later").setErrorData(seconds);
} }
if (!BCrypt.checkpw(password, user.getPassword())) { if (!BCrypt.checkpw(password, user.getPassword())) {
// If the password is mismatch // If the password is mismatch
// Add login failure count // Add login failure count

View File

@ -16,12 +16,8 @@ import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.Properties; import java.util.Properties;
import java.util.UUID; import java.util.UUID;
@ -37,28 +33,65 @@ import java.util.UUID;
@Slf4j @Slf4j
public class HaloUtils { public class HaloUtils {
@NonNull
public static String timeFormat(long totalSeconds) {
if (totalSeconds <= 0) {
return "0 second";
}
StringBuilder timeBuilder = new StringBuilder();
long hours = totalSeconds / 3600;
long minutes = totalSeconds % 3600 / 60;
long seconds = totalSeconds % 3600 % 60;
if (hours > 0) {
if (StringUtils.isNotBlank(timeBuilder)) {
timeBuilder.append(", ");
}
timeBuilder.append(pluralize(hours, "hour", "hours"));
}
if (minutes > 0) {
if (StringUtils.isNotBlank(timeBuilder)) {
timeBuilder.append(", ");
}
timeBuilder.append(pluralize(minutes, "minute", "minutes"));
}
if (seconds > 0) {
if (StringUtils.isNotBlank(timeBuilder)) {
timeBuilder.append(", ");
}
timeBuilder.append(pluralize(seconds, "second", "seconds"));
}
return timeBuilder.toString();
}
/** /**
* Pluralize the time label format. * Pluralize the times label format.
* *
* @param time time * @param times times
* @param label label * @param label label
* @param pluralLabel plural label * @param pluralLabel plural label
* @return pluralized format * @return pluralized format
*/ */
@NonNull @NonNull
public static String pluralize(long time, @NonNull String label, @NonNull String pluralLabel) { public static String pluralize(long times, @NonNull String label, @NonNull String pluralLabel) {
Assert.hasText(label, "Label must not be blank"); Assert.hasText(label, "Label must not be blank");
Assert.hasText(pluralLabel, "Plural label must not be blank"); Assert.hasText(pluralLabel, "Plural label must not be blank");
if (time <= 0) { if (times <= 0) {
return "no " + label; return "no " + label;
} }
if (time == 1) { if (times == 1) {
return time + " " + label; return times + " " + label;
} }
return time + " " + pluralLabel; return times + " " + pluralLabel;
} }
/** /**
@ -184,18 +217,13 @@ public class HaloUtils {
* @return * @return
*/ */
public static Date getCreateTime(String srcPath) { public static Date getCreateTime(String srcPath) {
final Path path = Paths.get(srcPath);
final BasicFileAttributeView basicview = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
BasicFileAttributes attr;
try { try {
attr = basicview.readAttributes(); BasicFileAttributes basicFileAttributes = Files.readAttributes(Paths.get(srcPath), BasicFileAttributes.class);
return new Date(attr.creationTime().toMillis()); basicFileAttributes.creationTime().toMillis();
} catch (Exception e) { return new Date(basicFileAttributes.creationTime().toMillis());
e.printStackTrace(); } catch (IOException e) {
throw new RuntimeException("Failed to open the " + srcPath + " file", e);
} }
final Calendar cal = Calendar.getInstance();
cal.set(1970, 0, 1, 0, 0, 0);
return cal.getTime();
} }
/** /**

View File

@ -16,6 +16,47 @@ import static org.junit.Assert.assertThat;
*/ */
public class HaloUtilsTest { public class HaloUtilsTest {
@Test
public void timeFormatTest() {
long seconds = 0;
String timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("0 second"));
seconds = -1;
timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("0 second"));
seconds = 30;
timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("30 seconds"));
seconds = 60;
timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("1 minute"));
seconds = 120;
timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("2 minutes"));
seconds = 3600;
timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("1 hour"));
seconds = 7200;
timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("2 hours"));
seconds = 7200 + 30;
timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("2 hours, 30 seconds"));
seconds = 7200 + 60 + 30;
timeFormat = HaloUtils.timeFormat(seconds);
assertThat(timeFormat, equalTo("2 hours, 1 minute, 30 seconds"));
}
@Test @Test
public void pluralizeTest() { public void pluralizeTest() {