Move setPermissionsSync to util

31cc05ae46a307ffa144392e495d7d2e2fad568a

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +37 -24Ignore whitespace
src/endpoints/content-manager.js+1 -24
@@ -7,7 +7,7 @@ import fetch from 'node-fetch';
77import sanitize from 'sanitize-filename';
88import { sync as writeFileAtomicSync } from 'write-file-atomic';
99
1010import { getConfigValue, color, setPermissionsSync } from '../util.js';
1111import { write } from '../character-card-parser.js';
1212import { serverDirectory } from '../server-directory.js';
1313
@@ -149,29 +149,6 @@ async function seedContentForUser(contentIndex, directories, forceCategories) {
149149 }
150150
151151 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- }
175152 setPermissionsSync(targetPath);
176153 console.info(`Content file ${contentItem.filename} copied to ${contentTarget}`);
177154 anyContentAdded = true;
src/util.js+36 -0
@@ -1087,3 +1087,39 @@ export function mutateJsonString(jsonString, mutation) {
10871087 return jsonString;
10881088 }
10891089}
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+}