mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-05-18 04:05:54 +00:00

# Description of Changes Please provide a summary of the changes, including: - What was changed - Why the change was made - Any challenges encountered Closes #(issue_number) --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing) for more details.
55 lines
1.9 KiB
Java
55 lines
1.9 KiB
Java
package stirling.software.SPDF.controller.web;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import stirling.software.SPDF.model.ApplicationProperties;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
@Service
|
|
@Slf4j
|
|
public class UploadLimitService {
|
|
|
|
@Autowired
|
|
private ApplicationProperties applicationProperties;
|
|
|
|
public long getUploadLimit() {
|
|
String maxUploadSize =
|
|
applicationProperties.getSystem().getFileUploadLimit() != null
|
|
? applicationProperties.getSystem().getFileUploadLimit()
|
|
: "";
|
|
|
|
if (maxUploadSize.isEmpty()) {
|
|
return 0;
|
|
} else if (!Pattern.compile("^[1-9][0-9]{0,2}[KMGkmg][Bb]$").matcher(maxUploadSize).matches()) {
|
|
log.error(
|
|
"Invalid maxUploadSize format. Expected format: [1-9][0-9]{0,2}[KMGkmg][Bb], but got: {}",
|
|
maxUploadSize);
|
|
return 0;
|
|
} else {
|
|
String unit = maxUploadSize.replaceAll("[1-9][0-9]{0,2}", "").toUpperCase();
|
|
String number = maxUploadSize.replaceAll("[KMGkmg][Bb]", "");
|
|
long size = Long.parseLong(number);
|
|
return switch (unit) {
|
|
case "KB" -> size * 1024;
|
|
case "MB" -> size * 1024 * 1024;
|
|
case "GB" -> size * 1024 * 1024 * 1024;
|
|
default -> 0;
|
|
};
|
|
}
|
|
}
|
|
|
|
//TODO: why do this server side not client?
|
|
public String getReadableUploadLimit() {
|
|
return humanReadableByteCount(getUploadLimit());
|
|
}
|
|
|
|
private String humanReadableByteCount(long bytes) {
|
|
if (bytes < 1024) return bytes + " B";
|
|
int exp = (int) (Math.log(bytes) / Math.log(1024));
|
|
String pre = "KMGTPE".charAt(exp - 1) + "B";
|
|
return String.format("%.1f %s", bytes / Math.pow(1024, exp), pre);
|
|
}
|
|
} |