mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08:00
Implementing media fail check.
Gallery now will stop indexing if the root folder is empty. That is probably unintentional and would erase the whole database.
This commit is contained in:
parent
e3bc01e41b
commit
5778ef00f7
@ -82,6 +82,14 @@ export class IndexingManager implements IIndexingManager {
|
|||||||
// eslint-disable-next-line no-async-promise-executor
|
// eslint-disable-next-line no-async-promise-executor
|
||||||
return new Promise(async (resolve, reject): Promise<void> => {
|
return new Promise(async (resolve, reject): Promise<void> => {
|
||||||
try {
|
try {
|
||||||
|
// Check if root is still a valid (non-empty) folder
|
||||||
|
// With weak devices it is possible that the media that stores
|
||||||
|
// the galley gets unmounted that triggers a full gallery wipe.
|
||||||
|
// Prevent it by stopping indexing on an empty folder.
|
||||||
|
if (fs.readdirSync(ProjectPath.ImageFolder).length === 0) {
|
||||||
|
return reject(new Error('Root directory is empty. This is probably error and would erase gallery database. Stopping indexing.'));
|
||||||
|
}
|
||||||
|
|
||||||
const scannedDirectory = await DiskManager.scanDirectory(
|
const scannedDirectory = await DiskManager.scanDirectory(
|
||||||
relativeDirectoryName
|
relativeDirectoryName
|
||||||
);
|
);
|
||||||
|
@ -21,7 +21,9 @@ import {DiskManager} from '../../../../../src/backend/model/DiskManger';
|
|||||||
import {AlbumManager} from '../../../../../src/backend/model/database/sql/AlbumManager';
|
import {AlbumManager} from '../../../../../src/backend/model/database/sql/AlbumManager';
|
||||||
import {SortingMethods} from '../../../../../src/common/entities/SortingMethods';
|
import {SortingMethods} from '../../../../../src/common/entities/SortingMethods';
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
|
const deepEqualInAnyOrder = require('deep-equal-in-any-order');
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
const chai = require('chai');
|
const chai = require('chai');
|
||||||
|
|
||||||
chai.use(deepEqualInAnyOrder);
|
chai.use(deepEqualInAnyOrder);
|
||||||
@ -53,9 +55,11 @@ class IndexingManagerTest extends IndexingManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// to help WebStorm to handle the test cases
|
// to help WebStorm to handle the test cases
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
declare let describe: any;
|
declare let describe: any;
|
||||||
declare const after: any;
|
declare const after: any;
|
||||||
declare const it: any;
|
declare const it: any;
|
||||||
|
// eslint-disable-next-line prefer-const
|
||||||
describe = DBTestHelper.describe();
|
describe = DBTestHelper.describe();
|
||||||
|
|
||||||
describe('IndexingManager', (sqlHelper: DBTestHelper) => {
|
describe('IndexingManager', (sqlHelper: DBTestHelper) => {
|
||||||
@ -155,6 +159,46 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('should stop indexing on empty folder', async () => {
|
||||||
|
const gm = new GalleryManagerTest();
|
||||||
|
|
||||||
|
ProjectPath.reset();
|
||||||
|
ProjectPath.ImageFolder = path.join(__dirname, '/../../../assets');
|
||||||
|
Config.Server.Threading.enabled = false;
|
||||||
|
|
||||||
|
await ObjectManagers.getInstance().IndexingManager.indexDirectory('.');
|
||||||
|
if (ObjectManagers.getInstance().IndexingManager.IsSavingInProgress) {
|
||||||
|
await ObjectManagers.getInstance().IndexingManager.SavingReady;
|
||||||
|
}
|
||||||
|
|
||||||
|
const directoryPath = GalleryManager.parseRelativeDirePath(
|
||||||
|
'.'
|
||||||
|
);
|
||||||
|
const conn = await SQLConnection.getConnection();
|
||||||
|
const selected = await gm.selectParentDir(conn, directoryPath.name,
|
||||||
|
directoryPath.parent);
|
||||||
|
await gm.fillParentDir(conn, selected);
|
||||||
|
|
||||||
|
|
||||||
|
expect(selected?.media?.length)
|
||||||
|
.to.be.greaterThan(0);
|
||||||
|
const tmpDir = path.join(__dirname, '/../../../tmp/rnd5sdf_emptyDir');
|
||||||
|
fs.mkdirSync(tmpDir);
|
||||||
|
ProjectPath.ImageFolder = tmpDir;
|
||||||
|
let notFailed = false;
|
||||||
|
try {
|
||||||
|
await ObjectManagers.getInstance().IndexingManager.indexDirectory('.');
|
||||||
|
notFailed = true;
|
||||||
|
} catch (e) {
|
||||||
|
// it expected to fail
|
||||||
|
}
|
||||||
|
if (notFailed) {
|
||||||
|
expect(true).to.equal(false, 'indexDirectory is expected to fail');
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should support case sensitive directory', async () => {
|
it('should support case sensitive directory', async () => {
|
||||||
const gm = new GalleryManagerTest();
|
const gm = new GalleryManagerTest();
|
||||||
const im = new IndexingManagerTest();
|
const im = new IndexingManagerTest();
|
||||||
@ -605,6 +649,7 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
fs.statSync = statSync;
|
fs.statSync = statSync;
|
||||||
});
|
});
|
||||||
@ -612,6 +657,7 @@ describe('IndexingManager', (sqlHelper: DBTestHelper) => {
|
|||||||
it('with re indexing severity low', async () => {
|
it('with re indexing severity low', async () => {
|
||||||
Config.Server.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
|
Config.Server.Indexing.reIndexingSensitivity = ReIndexingSensitivity.low;
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
fs.statSync = () => ({ctime: new Date(dirTime), mtime: new Date(dirTime)});
|
fs.statSync = () => ({ctime: new Date(dirTime), mtime: new Date(dirTime)});
|
||||||
const gm = new GalleryManagerTest();
|
const gm = new GalleryManagerTest();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user