Move setPermissionsSync to util
| @@ -7,7 +7,7 @@ import fetch from 'node-fetch'; | |||
| 7 | import sanitize from 'sanitize-filename'; | 7 | import sanitize from 'sanitize-filename'; |
| 8 | import { sync as writeFileAtomicSync } from 'write-file-atomic'; | 8 | import { sync as writeFileAtomicSync } from 'write-file-atomic'; |
| 9 | 9 | ||
| 10 | import { getConfigValue, color } from '../util.js'; | 10 | import { getConfigValue, color, setPermissionsSync } from '../util.js'; |
| 11 | import { write } from '../character-card-parser.js'; | 11 | import { write } from '../character-card-parser.js'; |
| 12 | import { serverDirectory } from '../server-directory.js'; | 12 | import { serverDirectory } from '../server-directory.js'; |
| 13 | 13 | ||
| @@ -149,29 +149,6 @@ async function seedContentForUser(contentIndex, directories, forceCategories) { | |||
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | fs.cpSync(contentPath, targetPath, { recursive: true, force: false }); | 151 | fs.cpSync(contentPath, targetPath, { recursive: true, force: false }); |
| 152 | function setPermissionsSync(targetPath_) { | ||
| 153 | |||
| 154 | function appendWritablePermission(filepath, stats) { | ||
| 155 | const currentMode = stats.mode; | ||
| 156 | const newMode = currentMode | 0o200; | ||
| 157 | if (newMode != currentMode) { | ||
| 158 | fs.chmodSync(filepath, newMode); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | const stats = fs.statSync(targetPath_); | ||
| 163 | |||
| 164 | if (stats.isDirectory()) { | ||
| 165 | appendWritablePermission(targetPath_, stats); | ||
| 166 | const files = fs.readdirSync(targetPath_); | ||
| 167 | |||
| 168 | files.forEach((file) => { | ||
| 169 | setPermissionsSync(path.join(targetPath_, file)); | ||
| 170 | }); | ||
| 171 | } else { | ||
| 172 | appendWritablePermission(targetPath_, stats); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | setPermissionsSync(targetPath); | 152 | setPermissionsSync(targetPath); |
| 176 | console.info(`Content file ${contentItem.filename} copied to ${contentTarget}`); | 153 | console.info(`Content file ${contentItem.filename} copied to ${contentTarget}`); |
| 177 | anyContentAdded = true; | 154 | anyContentAdded = true; |
| @@ -1087,3 +1087,39 @@ export function mutateJsonString(jsonString, mutation) { | |||
| 1087 | return jsonString; | 1087 | return jsonString; |
| 1088 | } | 1088 | } |
| 1089 | } | 1089 | } |
| 1090 | |||
| 1091 | /** | ||
| 1092 | * Sets the permissions of a file or directory to be writable. | ||
| 1093 | * @param {string} targetPath Path to the file or directory | ||
| 1094 | */ | ||
| 1095 | export function setPermissionsSync(targetPath) { | ||
| 1096 | /** | ||
| 1097 | * Appends writable permission to the file mode. | ||
| 1098 | * @param {string} filePath Path to the file | ||
| 1099 | * @param {fs.Stats} stats File stats | ||
| 1100 | */ | ||
| 1101 | function appendWritablePermission(filePath, stats) { | ||
| 1102 | const currentMode = stats.mode; | ||
| 1103 | const newMode = currentMode | 0o200; | ||
| 1104 | if (newMode != currentMode) { | ||
| 1105 | fs.chmodSync(filePath, newMode); | ||
| 1106 | } | ||
| 1107 | } | ||
| 1108 | |||
| 1109 | try { | ||
| 1110 | const stats = fs.statSync(targetPath); | ||
| 1111 | |||
| 1112 | if (stats.isDirectory()) { | ||
| 1113 | appendWritablePermission(targetPath, stats); | ||
| 1114 | const files = fs.readdirSync(targetPath); | ||
| 1115 | |||
| 1116 | files.forEach((file) => { | ||
| 1117 | setPermissionsSync(path.join(targetPath, file)); | ||
| 1118 | }); | ||
| 1119 | } else { | ||
| 1120 | appendWritablePermission(targetPath, stats); | ||
| 1121 | } | ||
| 1122 | } catch (error) { | ||
| 1123 | console.error(`Error setting write permissions for ${targetPath}:`, error); | ||
| 1124 | } | ||
| 1125 | } | ||