Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ApplicationRunner seedAdmin(
// Credentials are read from application.yml (environment-configurable).
// Once created, admin password is not auto-reset (prevent privilege escalation).
// To recover a lost admin password, manually set a new one via database or admin console.
boolean adminExists = userRepo.findAll().stream().anyMatch(User::isAdmin);
boolean adminExists = userRepo.existsByAdminTrue();
if (!adminExists) {
User admin = new User();
admin.setEmail(adminEmail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

import com.crystalpdf.backend.entity.Document;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface DocumentRepository extends JpaRepository<Document, Long> {
List<Document> findByOwnerIdOrderByCreatedAtDesc(Long userId);
Optional<Document> findByIdAndOwnerId(Long id, Long userId);

/** Sum storage without loading every Document row (list endpoint / quota checks). */
@Query("select coalesce(sum(d.sizeBytes), 0) from Document d where d.owner.id = :ownerId")
long sumSizeBytesByOwnerId(@Param("ownerId") Long ownerId);

long countByOwner_Id(Long ownerId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
boolean existsByEmail(String email);
Optional<User> findByUsername(String username);
boolean existsByAdminTrue();
long countByAdminTrue();
boolean existsByUsername(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ public Page<AdminUserResponse> getAllUsers(int page, int pageSize, String search

// Convert to responses
List<AdminUserResponse> responses = allUsers.stream().map(user -> {
List<Document> docs = documentRepository.findByOwnerIdOrderByCreatedAtDesc(user.getId());
long storageUsed = docs.stream().mapToLong(Document::getSizeBytes).sum();
long storageUsed = documentRepository.sumSizeBytesByOwnerId(user.getId());
int documentCount = (int) documentRepository.countByOwner_Id(user.getId());
AppSettings settings = getSettingsEntity();
long limitBytes = user.getStorageLimitBytes() != null ? user.getStorageLimitBytes()
: settings.getDefaultStorageLimitMb() * 1024L * 1024L;
return new AdminUserResponse(
user.getId(), user.getEmail(), user.getDisplayUsername(),
user.isAdmin(), user.isPasswordChangeRequired(),
limitBytes, storageUsed, docs.size(),
limitBytes, storageUsed, documentCount,
user.getCreatedAt() != null ? user.getCreatedAt().toString() : ""
);
}).toList();
Expand Down Expand Up @@ -167,7 +167,7 @@ public Map<String, Object> getSystemInfo() {
// Platform stats
info.put("totalUsers", userRepository.count());
info.put("totalFiles", documentRepository.count());
info.put("totalAdmins", userRepository.findAll().stream().filter(User::isAdmin).count());
info.put("totalAdmins", userRepository.countByAdminTrue());
info.put("javaVersion", System.getProperty("java.version"));
info.put("osName", System.getProperty("os.name"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ public Document store(MultipartFile file, User owner) throws IOException {
settings.getMaxUploadSizeMb() + " MB.");
}

// Check storage limit
List<Document> existingDocs = documentRepository.findByOwnerIdOrderByCreatedAtDesc(owner.getId());
long usedBytes = existingDocs.stream().mapToLong(Document::getSizeBytes).sum();
// Check storage limit (aggregate query — avoid loading all document rows)
long usedBytes = documentRepository.sumSizeBytesByOwnerId(owner.getId());
long limitBytes = owner.getStorageLimitBytes() != null ? owner.getStorageLimitBytes()
: settings.getDefaultStorageLimitMb() * 1024L * 1024L;
if (usedBytes + fileSizeBytes > limitBytes) {
Expand Down