Skip to content
Commits on Source (7)
# [1.41.0](https://gitlab.coko.foundation/ketida/server/compare/v1.40.0...v1.41.0) (2024-11-13)
### Bug Fixes
* **api/graphql:** delete old translation file for a language when uploading new one ([6b6e237](https://gitlab.coko.foundation/ketida/server/commit/6b6e2378df18b3ca0d5429064bd6322a59469a7a))
* **api/graphql:** use language code for translation file name ([44631eb](https://gitlab.coko.foundation/ketida/server/commit/44631eb239c706424b20400a17beb9aa407e17a1))
### Features
* add default english language strings for translations ([89e0505](https://gitlab.coko.foundation/ketida/server/commit/89e0505c5bca6852a77808a45f79652f5024964c))
# [1.40.0](https://gitlab.coko.foundation/ketida/server/compare/v1.39.0...v1.40.0) (2024-11-04)
......
......@@ -24,6 +24,7 @@ extend type Mutation {
updateApplicationParameters(
input: updateParametersInput!
): applicationParameter!
uploadTranslation(file: Upload, code: String): ID!
}
extend type Subscription {
......
const { logger } = require('@coko/server')
const { pubsubManager } = require('@coko/server')
const {
pubsubManager,
logger,
deleteFiles,
createFile,
} = require('@coko/server')
const fs = require('node:fs')
const path = require('node:path')
const { UPDATE_APPLICATION_PARAMETERS } = require('./constants')
const { File } = require('../../../models').models
const {
getApplicationParameters,
......@@ -50,12 +57,53 @@ const updateApplicationParametersHandler = async (_, { input }, ctx) => {
}
}
const uploadTranslationHandler = async (_, { file, code }) => {
const { createReadStream } = await file
// find and delete old file
const oldTranslationFile = await File.query()
.select('*')
.where('name', code)
.whereJsonSupersetOf('tags', ['translation'])
if (oldTranslationFile.length) {
await deleteFiles([oldTranslationFile[0].id])
}
const fileStream = createReadStream()
// ACL: 'public-read' in the params of fileStorage.uploadFileHandler() to make the url permanent
const { id } = await createFile(
fileStream,
code,
null,
null,
['translation'],
null,
)
// save a copy in the mounted folder
// NOTE: if the s3 file url would be permanent we wouldn't need to do this
await new Promise(resolve => {
createReadStream()
.pipe(
fs.createWriteStream(
path.join(__dirname, '../../../config/languages', `${code}.json`),
),
)
.on('close', resolve)
})
return id
}
module.exports = {
Query: {
getApplicationParameters: getApplicationParametersHandler,
},
Mutation: {
updateApplicationParameters: updateApplicationParametersHandler,
uploadTranslation: uploadTranslationHandler,
},
Subscription: {
updateApplicationParameters: {
......
......@@ -4,6 +4,8 @@ const config = require('config')
const mime = require('mime-types')
const get = require('lodash/get')
const { NotFoundError } = require('objection')
const express = require('express')
const path = require('node:path')
const uploadsDir = get(config, ['pubsweet-server', 'uploads'], 'uploads')
const { readFile } = require('../../utilities/filesystem')
......@@ -108,10 +110,10 @@ const RESTEndpoints = app => {
})
app.use('/api/fileserver/cleanup/:scope/:hash', async (req, res, next) => {
const { scope, hash } = req.params
const path = `${process.cwd()}/${uploadsDir}/${scope}/${hash}`
const filePath = `${process.cwd()}/${uploadsDir}/${scope}/${hash}`
try {
await fs.remove(path)
await fs.remove(filePath)
res.end()
} catch (error) {
res.status(500).json({ error: error.message })
......@@ -121,11 +123,11 @@ const RESTEndpoints = app => {
const { location, file } = req.params
try {
const path = `${process.cwd()}/${uploadsDir}/temp/previewer/${location}/${file}`
const filePath = `${process.cwd()}/${uploadsDir}/temp/previewer/${location}/${file}`
if (fs.existsSync(path)) {
const mimetype = mime.lookup(path)
const fileContent = await readFile(path, 'binary')
if (fs.existsSync(filePath)) {
const mimetype = mime.lookup(filePath)
const fileContent = await readFile(filePath, 'binary')
res.setHeader('Content-Type', `${mimetype}`)
res.setHeader('Content-Disposition', `attachment; filename=${file}`)
res.write(fileContent, 'binary')
......@@ -137,6 +139,10 @@ const RESTEndpoints = app => {
res.status(500).json({ error })
}
})
app.use(
'/languages',
express.static(path.join(__dirname, '../../', 'config/languages')),
)
}
module.exports = RESTEndpoints
This diff is collapsed.
......@@ -151,4 +151,5 @@ module.exports = {
enabled: true,
},
},
languages: [{ code: 'en', name: 'English', flagCode: 'gb' }],
}
{
"name": "server",
"version": "1.40.0",
"version": "1.41.0",
"private": true,
"description": "Ketida's Platform common server",
"repository": {
......@@ -44,6 +44,7 @@
"cheerio": "^1.0.0-rc.2",
"config": "^3.2.5",
"css-tree": "^1.1.2",
"express": "^4.17.1",
"form-data": "^3.0.0",
"fs-extra": "^9.0.1",
"graphql-subscriptions": "1.2.1",
......
......@@ -74,6 +74,7 @@ const seedApplicationParameters = async () => {
case 'aiEnabled':
case 'exportsConfig':
case 'chatGptApiKey':
case 'languages':
if (!existingParam) {
logger.info(
`Creating new Application Parameter: ${JSON.stringify(
......