Files

126 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2025-09-02 21:43:00 +10:00
import fs from "node:fs";
import errs from "../lib/error.js";
import settingModel from "../models/setting.js";
import internalNginx from "./nginx.js";
2020-02-19 15:55:06 +11:00
const internalSetting = {
/**
* @param {Access} access
* @param {Object} data
* @param {String} data.id
* @return {Promise}
*/
update: (access, data) => {
2025-09-02 21:43:00 +10:00
return access
.can("settings:update", data.id)
2020-02-19 15:55:06 +11:00
.then((/*access_data*/) => {
2025-09-02 21:43:00 +10:00
return internalSetting.get(access, { id: data.id });
2020-02-19 15:55:06 +11:00
})
.then((row) => {
if (row.id !== data.id) {
// Sanity check that something crazy hasn't happened
2025-09-02 21:43:00 +10:00
throw new errs.InternalValidationError(
`Setting could not be updated, IDs do not match: ${row.id} !== ${data.id}`,
);
2020-02-19 15:55:06 +11:00
}
2025-09-02 21:43:00 +10:00
return settingModel.query().where({ id: data.id }).patch(data);
2020-02-19 15:55:06 +11:00
})
.then(() => {
return internalSetting.get(access, {
2025-09-02 21:43:00 +10:00
id: data.id,
2020-02-19 15:55:06 +11:00
});
})
.then((row) => {
2025-09-02 21:43:00 +10:00
if (row.id === "default-site") {
2020-02-19 15:55:06 +11:00
// write the html if we need to
2025-09-02 21:43:00 +10:00
if (row.value === "html") {
fs.writeFileSync("/data/nginx/default_www/index.html", row.meta.html, { encoding: "utf8" });
2020-02-19 15:55:06 +11:00
}
// Configure nginx
2025-09-02 21:43:00 +10:00
return internalNginx
.deleteConfig("default")
2020-02-19 15:55:06 +11:00
.then(() => {
2025-09-02 21:43:00 +10:00
return internalNginx.generateConfig("default", row);
2020-02-19 15:55:06 +11:00
})
.then(() => {
return internalNginx.test();
})
.then(() => {
return internalNginx.reload();
})
.then(() => {
return row;
})
.catch((/*err*/) => {
2025-09-02 21:43:00 +10:00
internalNginx
.deleteConfig("default")
2020-02-19 15:55:06 +11:00
.then(() => {
return internalNginx.test();
})
.then(() => {
return internalNginx.reload();
})
.then(() => {
// I'm being slack here I know..
2025-09-02 21:43:00 +10:00
throw new errs.ValidationError("Could not reconfigure Nginx. Please check logs.");
2020-02-19 15:55:06 +11:00
});
});
}
2025-09-02 21:43:00 +10:00
return row;
2020-02-19 15:55:06 +11:00
});
},
/**
* @param {Access} access
* @param {Object} data
* @param {String} data.id
* @return {Promise}
*/
get: (access, data) => {
2025-09-02 21:43:00 +10:00
return access
.can("settings:get", data.id)
2020-02-19 15:55:06 +11:00
.then(() => {
2025-09-02 21:43:00 +10:00
return settingModel.query().where("id", data.id).first();
2020-02-19 15:55:06 +11:00
})
.then((row) => {
if (row) {
return row;
}
2025-09-02 21:43:00 +10:00
throw new errs.ItemNotFoundError(data.id);
2020-02-19 15:55:06 +11:00
});
},
/**
* This will only count the settings
*
* @param {Access} access
* @returns {*}
*/
getCount: (access) => {
2025-09-02 21:43:00 +10:00
return access
.can("settings:list")
2020-02-19 15:55:06 +11:00
.then(() => {
2025-09-02 21:43:00 +10:00
return settingModel.query().count("id as count").first();
2020-02-19 15:55:06 +11:00
})
.then((row) => {
2025-09-02 21:43:00 +10:00
return Number.parseInt(row.count, 10);
2020-02-19 15:55:06 +11:00
});
},
/**
* All settings
*
* @param {Access} access
* @returns {Promise}
*/
getAll: (access) => {
2025-09-02 21:43:00 +10:00
return access.can("settings:list").then(() => {
return settingModel.query().orderBy("description", "ASC");
});
},
2020-02-19 15:55:06 +11:00
};
2025-09-02 21:43:00 +10:00
export default internalSetting;