Skip to content

Commit

Permalink
making file reading issues only a warning bpatrik#731
Browse files Browse the repository at this point in the history
  • Loading branch information
bpatrik committed Oct 14, 2023
1 parent 81e7e7a commit 237e61c
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 99 deletions.
81 changes: 46 additions & 35 deletions src/backend/middlewares/RenderingMWs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const forcedDebug = process.env['NODE_ENV'] === 'debug';

export class RenderingMWs {
public static renderResult(
req: Request,
res: Response,
next: NextFunction
req: Request,
res: Response,
next: NextFunction
): void {
if (typeof req.resultPipe === 'undefined') {
return next();
Expand All @@ -26,9 +26,9 @@ export class RenderingMWs {
}

public static renderSessionUser(
req: Request,
res: Response,
next: NextFunction
req: Request,
res: Response,
next: NextFunction
): void {
if (!req.session['user']) {
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'User not exists'));
Expand All @@ -51,9 +51,9 @@ export class RenderingMWs {
}

public static renderSharing(
req: Request,
res: Response,
next: NextFunction
req: Request,
res: Response,
next: NextFunction
): void {
if (!req.resultPipe) {
return next();
Expand All @@ -65,9 +65,9 @@ export class RenderingMWs {
}

public static renderSharingList(
req: Request,
res: Response,
next: NextFunction
req: Request,
res: Response,
next: NextFunction
): void {
if (!req.resultPipe) {
return next();
Expand All @@ -82,9 +82,9 @@ export class RenderingMWs {
}

public static renderFile(
req: Request,
res: Response,
next: NextFunction
req: Request,
res: Response,
next: NextFunction
): void {
if (!req.resultPipe) {
return next();
Expand All @@ -96,52 +96,63 @@ export class RenderingMWs {
}

public static renderOK(
req: Request,
res: Response
req: Request,
res: Response
): void {
const message = new Message<string>(null, 'ok');
res.json(message);
}

public static async renderConfig(
req: Request,
res: Response
req: Request,
res: Response
): Promise<void> {
const originalConf = await Config.original();
// These are sensitive information, do not send to the client side
originalConf.Server.sessionSecret = null;
const message = new Message<PrivateConfigClass>(
null,
originalConf.toJSON({
attachState: true,
attachVolatile: true,
skipTags: {secret: true} as TAGS
}) as PrivateConfigClass
null,
originalConf.toJSON({
attachState: true,
attachVolatile: true,
skipTags: {secret: true} as TAGS
}) as PrivateConfigClass
);
res.json(message);
}

public static renderError(
err: Error,
req: Request,
res: Response,
next: NextFunction
err: Error,
req: Request,
res: Response,
next: NextFunction
): void {
if (err instanceof ErrorDTO) {
if (err.details) {
Logger.warn('Handled error:');
LoggerRouter.log(Logger.warn, req, res);
// use separate rendering for detailsStr
const d = err.detailsStr;
delete err.detailsStr;
console.log(err);
if (err.detailsStr) {
try {
console.log('details:', JSON.stringify(err.detailsStr));
} catch (_) {
console.log(err.detailsStr);
}
}
err.detailsStr = d;
delete err.details; // do not send back error object to the client side

// hide error details for non developers
if (
!(
forcedDebug ||
(req.session &&
req.session['user'] &&
req.session['user'].role >= UserRoles.Developer)
)
!(
forcedDebug ||
(req.session &&
req.session['user'] &&
req.session['user'].role >= UserRoles.Developer)
)
) {
delete err.detailsStr;
}
Expand Down
156 changes: 92 additions & 64 deletions src/backend/model/fileaccess/DiskManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {Utils} from '../../../common/Utils';
import {GPXProcessing} from './fileprocessing/GPXProcessing';
import {MDFileDTO} from '../../../common/entities/MDFileDTO';
import {MetadataLoader} from './MetadataLoader';
import {NotificationManager} from '../NotifocationManager';


const LOG_TAG = '[DiskManager]';
Expand Down Expand Up @@ -148,66 +149,83 @@ export class DiskManager {
path.join(absoluteDirectoryName, file)
);
if ((await fsp.stat(fullFilePath)).isDirectory()) {
if (
settings.noDirectory === true ||
settings.coverOnly === true ||
(await DiskManager.excludeDir(
file,
relativeDirectoryName,
absoluteDirectoryName
))
) {
continue;
}

// create cover directory
const d = (await DiskManager.scanDirectory(
path.join(relativeDirectoryName, file),
{
coverOnly: true,
try {
if (
settings.noDirectory === true ||
settings.coverOnly === true ||
(await DiskManager.excludeDir(
file,
relativeDirectoryName,
absoluteDirectoryName
))
) {
continue;
}
)) as SubDirectoryDTO;

directory.directories.push(d);
} else if (PhotoProcessing.isPhoto(fullFilePath)) {
if (settings.noPhoto === true) {
continue;
// create cover directory
const d = (await DiskManager.scanDirectory(
path.join(relativeDirectoryName, file),
{
coverOnly: true,
}
)) as SubDirectoryDTO;

directory.directories.push(d);
} catch (err) {
NotificationManager.warning(
'Unknown directory reading error, skipping: ' + path.join(relativeDirectoryName, file),
err.toString()
);
console.error(err);
}
} else if (PhotoProcessing.isPhoto(fullFilePath)) {
try {
if (settings.noPhoto === true) {
continue;
}

const photo = {
name: file,
directory: null,
metadata:
settings.noMetadata === true
? null
: await MetadataLoader.loadPhotoMetadata(fullFilePath),
} as PhotoDTO;

if (!directory.cover) {
directory.cover = Utils.clone(photo);

directory.cover.directory = {
path: directory.path,
name: directory.name,
};
}
// add the cover photo to the list of media, so it will be saved to the DB
// and can be queried to populate covers,
// otherwise we do not return media list that is only partial
directory.media.push(photo);
const photo = {
name: file,
directory: null,
metadata:
settings.noMetadata === true
? null
: await MetadataLoader.loadPhotoMetadata(fullFilePath),
} as PhotoDTO;

if (!directory.cover) {
directory.cover = Utils.clone(photo);

directory.cover.directory = {
path: directory.path,
name: directory.name,
};
}
// add the cover photo to the list of media, so it will be saved to the DB
// and can be queried to populate covers,
// otherwise we do not return media list that is only partial
directory.media.push(photo);

if (settings.coverOnly === true) {
break;
if (settings.coverOnly === true) {
break;
}
} catch (err) {
NotificationManager.warning('Media loading error, skipping: ' +
fullFilePath +
', reason: ' +
err.toString()
);
console.error(err);
}
} else if (VideoProcessing.isVideo(fullFilePath)) {
if (
Config.Media.Video.enabled === false ||
settings.noVideo === true ||
settings.coverOnly === true
) {
continue;
}
try {
if (
Config.Media.Video.enabled === false ||
settings.noVideo === true ||
settings.coverOnly === true
) {
continue;
}
directory.media.push({
name: file,
directory: null,
Expand All @@ -219,24 +237,34 @@ export class DiskManager {
} catch (e) {
Logger.warn(
'Media loading error, skipping: ' +
file +
fullFilePath +
', reason: ' +
e.toString()
);
}
} else if (GPXProcessing.isMetaFile(fullFilePath)) {
if (
!DiskManager.isEnabledMetaFile(fullFilePath) ||
settings.noMetaFile === true ||
settings.coverOnly === true
) {
continue;
}

directory.metaFile.push({
name: file,
directory: null,
} as FileDTO);
try {
if (
!DiskManager.isEnabledMetaFile(fullFilePath) ||
settings.noMetaFile === true ||
settings.coverOnly === true
) {
continue;
}

directory.metaFile.push({
name: file,
directory: null,
} as FileDTO);
} catch (e) {
Logger.warn(
'Metafile loading error, skipping: ' +
fullFilePath +
', reason: ' +
e.toString()
);
}
}
}

Expand Down

0 comments on commit 237e61c

Please sign in to comment.