Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/1511661.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: 1511661

on:
push:
branches:
- f24
- 'feature/*'
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '20'

- name: Install dependencies
run: npm install

- name: Compile TypeScript
run: npx tsc

- name: Run tests (opcional)
run: npm test
Empty file modified .husky/commit-msg
100755 → 100644
Empty file.
Empty file modified .husky/pre-commit
100755 → 100644
Empty file.
Empty file modified install/docker/entrypoint.sh
100755 → 100644
Empty file.
Empty file modified nodebb
100755 → 100644
Empty file.
121 changes: 121 additions & 0 deletions src/groups/cover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
'use strict';

import path from 'path';
import nconf from 'nconf';
import db from '../database'; // Módulo JS posiblemente sin migrar a TS
import image from '../image'; // Módulo JS posiblemente sin migrar a TS
import file from '../file'; // Módulo JS posiblemente sin migrar a TS

// Inicializa nconf
nconf.argv().env().file({ file: path.join(__dirname, '../../node_modules/nconf/lib/config.json') });

// Tipos de datos necesarios
interface CoverData {
file?: {
path: string;
type: string;
};
imageData?: string;
position?: string;
groupName: string;
}

interface UploadData {
url: string;
}

// Función principal
export default function (Groups: any) {
const allowedTypes = ['image/png', 'image/jpeg', 'image/bmp'];

// Función para actualizar la posición de la imagen de portada
Groups.updateCoverPosition = async function (groupName: string, position: string): Promise<void> {
if (!groupName) {
throw new Error('[[error:invalid-data]]');
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
await Groups.setGroupField(groupName, 'cover:position', position);
};

// Función para actualizar la imagen de portada
Groups.updateCover = async function (uid: number, data: CoverData): Promise<{ url: string }> {
let tempPath = data.file ? data.file.path : '';
try {
if (!data.imageData && !data.file && data.position) {
return await Groups.updateCoverPosition(data.groupName, data.position);
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
const type = data.file ? data.file.type : image.mimeFromBase64(data.imageData || '');
if (!type || !allowedTypes.includes(type)) {
throw new Error('[[error:invalid-image]]');
}

if (!tempPath) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
tempPath = await image.writeImageDataToTempFile(data.imageData as string);
}

const filename = `groupCover-${data.groupName}${path.extname(tempPath)}`;

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const uploadData: UploadData = await image.uploadImage(filename, 'files', {
path: tempPath,
uid: uid,
name: 'groupCover',
});

const { url } = uploadData;

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
await Groups.setGroupField(data.groupName, 'cover:url', url);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await image.resizeImage({
path: tempPath,
width: 358,
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const thumbUploadData: UploadData = await image.uploadImage(`groupCoverThumb-${data.groupName}${path.extname(tempPath)}`, 'files', {
path: tempPath,
uid: uid,
name: 'groupCover',
});

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
await Groups.setGroupField(data.groupName, 'cover:thumb:url', thumbUploadData.url);

if (data.position) {
await Groups.updateCoverPosition(data.groupName, data.position);
}

return { url: url };
} finally {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
file.delete(tempPath);
}
};

// Función para eliminar la imagen de portada
Groups.removeCover = async function (data: { groupName: string }): Promise<void> {
const fields = ['cover:url', 'cover:thumb:url'];
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const values = await Groups.getGroupFields(data.groupName, fields);

await Promise.all(fields.map((field) => {
if (!values[field] || !values[field].startsWith(`${nconf.get('relative_path')}/assets/uploads/files/`)) {
return;
}

const filename = values[field].split('/').pop();
const filePath = path.join(nconf.get('upload_path'), 'files', filename);

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
return file.delete(filePath);
}));

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await db.deleteObjectFields(`group:${data.groupName}`, ['cover:url', 'cover:thumb:url', 'cover:position']);
};
}
15 changes: 8 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"compilerOptions": {
"allowJs": false,
"allowJs": true,
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"outDir": "./dist",
"rootDir": "./src",
"noImplicitAny": false,
"esModuleInterop": true
},
"include": [
"public/src/**/*",
"src/**/*",
"test/**/*",
"test/**/*"
],
"exclude":[
"node_modules",
]
}
"exclude": ["node_modules"]
}