Skip to content

Commit 4e9cf79

Browse files
authored
fix(file): update file type and size retrieval to use primaryFile structure (#383)
1 parent be36d50 commit 4e9cf79

3 files changed

Lines changed: 19 additions & 21 deletions

File tree

booklore-api/src/main/java/org/booklore/service/BookRuleEvaluatorService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ private Expression<?> getFieldExpression(RuleField field, CriteriaBuilder cb, Ro
771771
case DATE_FINISHED -> progressJoin.get("dateFinished");
772772
case LAST_READ_TIME -> progressJoin.get("lastReadTime");
773773
case PERSONAL_RATING -> progressJoin.get("personalRating");
774-
case FILE_SIZE -> root.get("fileSizeKb");
774+
case FILE_SIZE -> root.join("bookFiles", JoinType.LEFT).get("fileSizeKb");
775775
case METADATA_SCORE -> root.get("metadataMatchScore");
776776
case TITLE -> root.get("metadata").get("title");
777777
case SUBTITLE -> root.get("metadata").get("subtitle");
@@ -814,7 +814,7 @@ private Expression<?> getFieldExpression(RuleField field, CriteriaBuilder cb, Ro
814814
yield cb.function("GREATEST", Float.class, koreader, kobo, pdf, epub, cbx);
815815
}
816816
case FILE_TYPE -> cb.function("SUBSTRING_INDEX", String.class,
817-
root.get("fileName"), cb.literal("."), cb.literal(-1));
817+
root.join("bookFiles", JoinType.LEFT).get("fileName"), cb.literal("."), cb.literal(-1));
818818
default -> null;
819819
};
820820
}

frontend/src/app/features/magic-shelf/service/book-rule-evaluator.service.spec.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ describe('BookRuleEvaluatorService', () => {
1414

1515
const createBook = (overrides: Partial<Book> = {}): Book => ({
1616
id: 1,
17-
bookType: 'EPUB',
17+
primaryFile: {id: 1, bookId: 1, bookType: 'EPUB', fileName: 'test.epub', filePath: '/path/to/test.epub', fileSizeKb: 1024},
1818
libraryId: 1,
1919
libraryName: 'Test Library',
20-
fileName: 'test.epub',
21-
filePath: '/path/to/test.epub',
2220
readStatus: ReadStatus.UNREAD,
2321
shelves: [],
2422
metadata: {
@@ -33,7 +31,7 @@ describe('BookRuleEvaluatorService', () => {
3331

3432
describe('fileType filtering', () => {
3533
it('should filter EPUB books correctly when rule uses "epub"', () => {
36-
const book = createBook({bookType: 'EPUB'});
34+
const book = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'EPUB'}});
3735
const group: GroupRule = {
3836
name: 'test',
3937
type: 'group',
@@ -52,7 +50,7 @@ describe('BookRuleEvaluatorService', () => {
5250
});
5351

5452
it('should filter PDF books correctly when rule uses "pdf"', () => {
55-
const book = createBook({bookType: 'PDF'});
53+
const book = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'PDF'}});
5654
const group: GroupRule = {
5755
name: 'test',
5856
type: 'group',
@@ -71,7 +69,7 @@ describe('BookRuleEvaluatorService', () => {
7169
});
7270

7371
it('should handle CBX books correctly for cbr, cbz, and cb7 rules', () => {
74-
const book = createBook({bookType: 'CBX'});
72+
const book = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'CBX'}});
7573

7674
const cbrGroup: GroupRule = {
7775
name: 'test',
@@ -117,7 +115,7 @@ describe('BookRuleEvaluatorService', () => {
117115
});
118116

119117
it('should handle not_equals operator correctly', () => {
120-
const epubBook = createBook({bookType: 'EPUB'});
118+
const epubBook = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'EPUB'}});
121119
const group: GroupRule = {
122120
name: 'test',
123121
type: 'group',
@@ -134,14 +132,14 @@ describe('BookRuleEvaluatorService', () => {
134132
const result = service.evaluateGroup(epubBook, group);
135133
expect(result).toBe(true);
136134

137-
const pdfBook = createBook({bookType: 'PDF'});
135+
const pdfBook = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'PDF'}});
138136
const result2 = service.evaluateGroup(pdfBook, group);
139137
expect(result2).toBe(false);
140138
});
141139

142140
it('should filter EPUB books correctly when rule uses "not_equals" with "epub"', () => {
143-
const epubBook = createBook({bookType: 'EPUB'});
144-
const pdfBook = createBook({bookType: 'PDF'});
141+
const epubBook = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'EPUB'}});
142+
const pdfBook = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'PDF'}});
145143

146144
const group: GroupRule = {
147145
name: 'test',
@@ -165,7 +163,7 @@ describe('BookRuleEvaluatorService', () => {
165163
describe('evaluateGroup', () => {
166164
it('should evaluate group rules with AND logic', () => {
167165
const book = createBook({
168-
bookType: 'EPUB'
166+
primaryFile: {id: 1, bookId: 1, bookType: 'EPUB'}
169167
});
170168

171169
const group: GroupRule = {
@@ -183,7 +181,7 @@ describe('BookRuleEvaluatorService', () => {
183181
});
184182

185183
it('should evaluate group rules with OR logic', () => {
186-
const book = createBook({bookType: 'PDF'});
184+
const book = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'PDF'}});
187185

188186
const group: GroupRule = {
189187
name: 'test',
@@ -1177,7 +1175,7 @@ describe('BookRuleEvaluatorService', () => {
11771175
});
11781176

11791177
it('should handle includes_any with fileType mapping', () => {
1180-
const book = createBook({bookType: 'CBX'});
1178+
const book = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'CBX'}});
11811179
expect(service.evaluateGroup(book, rule('fileType', 'includes_any', ['cbr', 'pdf']))).toBe(true);
11821180
});
11831181

@@ -1189,17 +1187,17 @@ describe('BookRuleEvaluatorService', () => {
11891187

11901188
describe('fileType mapping edge cases', () => {
11911189
it('should map azw to azw3', () => {
1192-
const book = createBook({bookType: 'AZW3'});
1190+
const book = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'AZW3'}});
11931191
expect(service.evaluateGroup(book, rule('fileType', 'equals', 'azw'))).toBe(true);
11941192
});
11951193

11961194
it('should handle MOBI bookType', () => {
1197-
const book = createBook({bookType: 'MOBI'});
1195+
const book = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'MOBI'}});
11981196
expect(service.evaluateGroup(book, rule('fileType', 'equals', 'mobi'))).toBe(true);
11991197
});
12001198

12011199
it('should handle not_equals with fileType cbr mapping', () => {
1202-
const book = createBook({bookType: 'PDF'});
1200+
const book = createBook({primaryFile: {id: 1, bookId: 1, bookType: 'PDF'}});
12031201
expect(service.evaluateGroup(book, rule('fileType', 'not_equals', 'cbr'))).toBe(true);
12041202
});
12051203
});

frontend/src/app/features/magic-shelf/service/book-rule-evaluator.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export class BookRuleEvaluatorService {
7171
case 'readStatus':
7272
return [String(book.readStatus ?? 'UNSET').toLowerCase()];
7373
case 'fileType':
74-
return [String(book['bookType'] ?? '').toLowerCase()];
74+
return [String(book.primaryFile?.bookType ?? '').toLowerCase()];
7575
case 'library':
7676
return [String(book.libraryId)];
7777
case 'shelf':
@@ -275,9 +275,9 @@ export class BookRuleEvaluatorService {
275275
case 'readStatus':
276276
return book.readStatus ?? 'UNSET';
277277
case 'fileType':
278-
return (book['bookType'] as string)?.toLowerCase() ?? null;
278+
return (book.primaryFile?.bookType as string)?.toLowerCase() ?? null;
279279
case 'fileSize':
280-
return book.fileSizeKb;
280+
return book.primaryFile?.fileSizeKb;
281281
case 'metadataScore':
282282
return book.metadataMatchScore;
283283
case 'personalRating':

0 commit comments

Comments
 (0)