Fix:Check if Windows before cleaning file path for POSIX separators #1254

This commit is contained in:
advplyr 2023-01-05 17:45:27 -06:00
parent f76f9c7f84
commit 9a85ad1f6b
19 changed files with 66 additions and 54 deletions

View file

@ -5,6 +5,7 @@ const njodb = require('../libs/njodb')
const { SupportedEbookTypes } = require('./globals')
const { PlayMethod } = require('./constants')
const { getId } = require('./index')
const { filePathToPOSIX } = require('./fileUtils')
const Logger = require('../Logger')
const Library = require('../objects/Library')
@ -87,8 +88,8 @@ function makeSeriesFromOldAb({ series, volumeNumber }) {
}
function getRelativePath(srcPath, basePath) {
srcPath = srcPath.replace(/\\/g, '/')
basePath = basePath.replace(/\\/g, '/')
srcPath = filePathToPOSIX(srcPath)
basePath = filePathToPOSIX(basePath)
return srcPath.replace(basePath, '')
}

View file

@ -3,10 +3,11 @@ const fs = require('../libs/fsExtra')
const Path = require('path')
const package = require('../../package.json')
const Logger = require('../Logger')
const { filePathToPOSIX } = require('./fileUtils')
function escapeSingleQuotes(path) {
// return path.replace(/'/g, '\'\\\'\'')
return path.replace(/\\/g, '/').replace(/ /g, '\\ ').replace(/'/g, '\\\'')
return filePathToPOSIX(path).replace(/ /g, '\\ ').replace(/'/g, '\\\'')
}
// Returns first track start time

View file

@ -80,11 +80,11 @@ function bytesPretty(bytes, decimals = 0) {
module.exports.bytesPretty = bytesPretty
async function recurseFiles(path, relPathToReplace = null) {
path = path.replace(/\\/g, '/')
path = this.filePathToPOSIX(path)
if (!path.endsWith('/')) path = path + '/'
if (relPathToReplace) {
relPathToReplace = relPathToReplace.replace(/\\/g, '/')
relPathToReplace = this.filePathToPOSIX(relPathToReplace)
if (!relPathToReplace.endsWith('/')) relPathToReplace += '/'
} else {
relPathToReplace = path
@ -244,4 +244,19 @@ module.exports.removeFile = (path) => {
Logger.error(`[fileUtils] Failed remove file "${path}"`, error)
return false
})
}
/**
* Make sure folder separator is POSIX for Windows file paths. e.g. "C:\Users\Abs" becomes "C:/Users/Abs"
*
* @param {String} path - Ugly file path
* @return {String} Pretty posix file path
*/
module.exports.filePathToPOSIX = (path) => {
if (!global.isWin || !path) return path
return path.replace(/\\/g, '/')
}
module.exports.encodeUriPath = (path) => {
return this.filePathToPOSIX(path).replace(/%/g, '%25').replace(/#/g, '%23')
}

View file

@ -1,5 +1,4 @@
const Path = require('path')
const fs = require('fs')
const Logger = require('../Logger')
const { parseString } = require("xml2js")
const areEquivalent = require('./areEquivalent')
@ -125,10 +124,6 @@ module.exports.copyValue = (val) => {
}
}
module.exports.encodeUriPath = (path) => {
return path.replace(/\\/g, '/').replace(/%/g, '%25').replace(/#/g, '%23')
}
module.exports.toNumber = (val, fallback = 0) => {
if (isNaN(val) || val === null) return fallback
return Number(val)

View file

@ -1,7 +1,7 @@
const Path = require('path')
const fs = require('../libs/fsExtra')
const Logger = require('../Logger')
const { recurseFiles, getFileTimestampsWithIno } = require('./fileUtils')
const { recurseFiles, getFileTimestampsWithIno, filePathToPOSIX } = require('./fileUtils')
const globals = require('./globals')
const LibraryFile = require('../objects/files/LibraryFile')
@ -176,7 +176,7 @@ function cleanFileObjects(libraryItemPath, files) {
// Scan folder
async function scanFolder(libraryMediaType, folder, serverSettings = {}) {
const folderPath = folder.fullPath.replace(/\\/g, '/')
const folderPath = filePathToPOSIX(folder.fullPath)
const pathExists = await fs.pathExists(folderPath)
if (!pathExists) {
@ -243,7 +243,7 @@ module.exports.scanFolder = scanFolder
// Input relative filepath, output all details that can be parsed
function getBookDataFromDir(folderPath, relPath, parseSubtitle = false) {
relPath = relPath.replace(/\\/g, '/')
relPath = filePathToPOSIX(relPath)
var splitDir = relPath.split('/')
var folder = splitDir.pop() // Audio files will always be in the directory named for the title
@ -333,7 +333,7 @@ function getSubtitle(folder) {
}
function getPodcastDataFromDir(folderPath, relPath) {
relPath = relPath.replace(/\\/g, '/')
relPath = filePathToPOSIX(relPath)
const splitDir = relPath.split('/')
// Audio files will always be in the directory named for the title
@ -360,8 +360,8 @@ function getDataFromMediaDir(libraryMediaType, folderPath, relPath, serverSettin
// Called from Scanner.js
async function getLibraryItemFileData(libraryMediaType, folder, libraryItemPath, isSingleMediaItem, serverSettings = {}) {
libraryItemPath = libraryItemPath.replace(/\\/g, '/')
const folderFullPath = folder.fullPath.replace(/\\/g, '/')
libraryItemPath = filePathToPOSIX(libraryItemPath)
const folderFullPath = filePathToPOSIX(folder.fullPath)
const libraryItemDir = libraryItemPath.replace(folderFullPath, '').slice(1)
let libraryItemData = {}