diff --git a/index.js b/index.js index b10b2d9..a6e7fdf 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ -import { Telegraf, Markup } from 'telegraf'; -import LocalSession from 'telegraf-session-local'; +const { Telegraf, Markup } = require('telegraf'); +const LocalSession = require('telegraf-session-local'); -import * as filesystem from './src/filesystem.js'; -import * as constants from './src/constants.js'; -import * as helpers from './src/helpers.js'; +const filesystem = require('./src/filesystem.js'); +const constants = require('./src/constants.js'); +const helpers = require('./src/helpers.js'); const fileHandler = async (ctx, fileType) => { console.log('handling file of type:', fileType); @@ -421,4 +421,7 @@ bot.on('text', async (ctx) => { process.once('SIGINT', () => bot.stop('SIGINT')); process.once('SIGTERM', () => bot.stop('SIGTERM')); -export default bot; \ No newline at end of file +module.exports.setBotToken = (token) => { + bot.telegram.token = token; +}; +module.exports.infiniteCloudBot = bot; \ No newline at end of file diff --git a/package.json b/package.json index 33aff0a..325590b 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "author": "Luca8991", "license": "MIT", "private": false, - "type": "module", "dependencies": { "node-fetch": "^2.6.1", "telegraf": "^4.3.0", diff --git a/src/constants.js b/src/constants.js index 69b4009..5b5ab9f 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,48 +1,89 @@ -import { Markup } from 'telegraf'; +const { Markup } = require('telegraf'); -export const dashboardUrl = 'https://infinitecloud-website-api.web.app/'; +const dashboardUrl = 'https://infinitecloud-website-api.web.app/'; /* ACTIONS */ -export const thisDirAction = '.'; -export const parentDirAction = '..'; -export const mkdirAction = 'mkdir'; -export const cancelAction = 'cancel'; -export const backAction = 'back'; -export const deleteAction = 'delete'; -export const SAVE_FILE_ACTION = 'save-file-action'; -export const MKDIR_ACTION = 'mkdir-action'; -export const WAIT_DIRECTORY_NAME = 'wait-directory-name'; -export const WAIT_FILE_NAME = 'wait-file-name'; -export const EXPLORER_ACTION = 'explorer-action'; -export const DELETE_DIR_ACTION = 'delete-dir-action'; -export const DELETE_FILE_ACTION = 'delete-file-action'; -export const RENAME_FILE_ACTION = 'rename-file-action'; -export const SELECT_MOVE_FILE_ACTION = 'select-move-file-action'; -export const MOVE_FILE_ACTION = 'move-file-action'; -export const fileActionPrefix = '/'; +const thisDirAction = '.'; +const parentDirAction = '..'; +const mkdirAction = 'mkdir'; +const cancelAction = 'cancel'; +const backAction = 'back'; +const deleteAction = 'delete'; +const SAVE_FILE_ACTION = 'save-file-action'; +const MKDIR_ACTION = 'mkdir-action'; +const WAIT_DIRECTORY_NAME = 'wait-directory-name'; +const WAIT_FILE_NAME = 'wait-file-name'; +const EXPLORER_ACTION = 'explorer-action'; +const DELETE_DIR_ACTION = 'delete-dir-action'; +const DELETE_FILE_ACTION = 'delete-file-action'; +const RENAME_FILE_ACTION = 'rename-file-action'; +const SELECT_MOVE_FILE_ACTION = 'select-move-file-action'; +const MOVE_FILE_ACTION = 'move-file-action'; +const fileActionPrefix = '/'; /* SYSTEM MESSAGES */ -export const currentPathMessage = 'CURRENT PATH:\n'; -export const saveFileMessage = 'SAVE FILE:\n'; -export const createDirMessage = 'CREATE DIRECTORY:\n'; -export const askDirectoryNameMessage = 'Input DIRECTORY name:\n'; -export const askFileNameMessage = 'Input FILE name:\n'; -export const deleteDirMessage = 'DELETE DIRECTORY:\n'; -export const deleteFileMessage = 'DELETE FILE:\n'; -export const moveFileMessage = 'MOVE FILE:\n'; +const currentPathMessage = 'CURRENT PATH:\n'; +const saveFileMessage = 'SAVE FILE:\n'; +const createDirMessage = 'CREATE DIRECTORY:\n'; +const askDirectoryNameMessage = 'Input DIRECTORY name:\n'; +const askFileNameMessage = 'Input FILE name:\n'; +const deleteDirMessage = 'DELETE DIRECTORY:\n'; +const deleteFileMessage = 'DELETE FILE:\n'; +const moveFileMessage = 'MOVE FILE:\n'; -export const fileSystemNotFound = 'Filesystem not found'; +const fileSystemNotFound = 'Filesystem not found'; /* INLINE BUTTONS */ -export const mkdirInlineButton = Markup.button.callback( +const mkdirInlineButton = Markup.button.callback( '+ New Directory', mkdirAction ); -export const parentDirInlineButton = Markup.button.callback('..', parentDirAction); -export const thisDirInlineButton = Markup.button.callback('.', thisDirAction); -export const cancelOperationInlineButton = Markup.button.callback( +const parentDirInlineButton = Markup.button.callback('..', parentDirAction); +const thisDirInlineButton = Markup.button.callback('.', thisDirAction); +const cancelOperationInlineButton = Markup.button.callback( 'Cancel', cancelAction ); -export const backInlineButton = Markup.button.callback('<< Back', backAction); -export const deleteInlineButton = Markup.button.callback('🗑️ DELETE THIS DIR', deleteAction); \ No newline at end of file +const backInlineButton = Markup.button.callback('<< Back', backAction); +const deleteInlineButton = Markup.button.callback( + '🗑️ DELETE THIS DIR', + deleteAction +); + +module.exports = { + thisDirAction, + parentDirAction, + mkdirAction, + cancelAction, + backAction, + deleteAction, + SAVE_FILE_ACTION, + MKDIR_ACTION, + WAIT_DIRECTORY_NAME, + WAIT_FILE_NAME, + EXPLORER_ACTION, + DELETE_DIR_ACTION, + DELETE_FILE_ACTION, + RENAME_FILE_ACTION, + SELECT_MOVE_FILE_ACTION, + MOVE_FILE_ACTION, + fileActionPrefix, + + currentPathMessage, + saveFileMessage, + createDirMessage, + askDirectoryNameMessage, + askFileNameMessage, + deleteDirMessage, + deleteFileMessage, + moveFileMessage, + + fileSystemNotFound, + + mkdirInlineButton, + parentDirInlineButton, + thisDirInlineButton, + cancelOperationInlineButton, + backInlineButton, + deleteInlineButton, +}; diff --git a/src/filesystem.js b/src/filesystem.js index 1a0dd85..f341881 100644 --- a/src/filesystem.js +++ b/src/filesystem.js @@ -1,12 +1,12 @@ -import * as fs from 'fs'; -import fetch from 'node-fetch'; -import { Markup } from 'telegraf'; -import { +const fs = require('fs'); +const fetch = require('node-fetch'); +const { Markup } = require('telegraf'); +const { deleteInlineButton, fileActionPrefix, parentDirInlineButton, thisDirAction, -} from './constants.js'; +} = require('./constants.js'); //const fileSystemMessage = 'FILESYSTEM - DO NOT DELETE, EDIT OR UNPIN THIS MESSAGE\n'; const FILESYSTEM_INIT = { @@ -44,7 +44,7 @@ const unpinOldFilesystem = async (ctx) => { } }; -export const storeFileSystem = async (ctx, fileSystem) => { +const storeFileSystem = async (ctx, fileSystem) => { const chatId = ctx.chat.id; ctx.session.filesystem = fileSystem; fs.writeFileSync(`filesystem${chatId}.json`, JSON.stringify(fileSystem)); @@ -62,7 +62,7 @@ export const storeFileSystem = async (ctx, fileSystem) => { }); }; -export const initializeFileSystem = async (ctx) => { +const initializeFileSystem = async (ctx) => { await storeFileSystem(ctx, FILESYSTEM_INIT); }; @@ -76,7 +76,7 @@ const fetchFileSystemObj = async (ctx, rootMessage) => { return filesystem; }; -export const getFileSystem = async (ctx) => { +const getFileSystem = async (ctx) => { const sessionFilesystem = ctx.session.filesystem; if (!sessionFilesystem) { const chat = await ctx.getChat(); @@ -98,7 +98,7 @@ const getDirectory = (fileSystem, path) => { return currentDirectory; }; -export const getElementsInPath = ( +const getElementsInPath = ( fileSystem, path, hideCurrentDirectory, @@ -138,7 +138,7 @@ export const getElementsInPath = ( return elements; }; -export const getKeyboardDirectories = async ( +const getKeyboardDirectories = async ( ctx, currentPath, hideCurrentDirectory = false, @@ -166,7 +166,7 @@ export const getKeyboardDirectories = async ( return inlineKeyboardButtons; }; -export const getParentDirectoryPath = (path) => { +const getParentDirectoryPath = (path) => { const pieces = path.split('/').filter((piece) => piece !== ''); pieces.pop(); if (pieces.length === 0) { @@ -176,14 +176,14 @@ export const getParentDirectoryPath = (path) => { return '/' + pieces.join('/') + '/'; }; -export const mkdir = async (ctx, targetPath, directoryName) => { +const mkdir = async (ctx, targetPath, directoryName) => { const fileSystem = await getFileSystem(ctx); const targetDirectory = getDirectory(fileSystem, targetPath); targetDirectory[directoryName] = { '.': [] }; await storeFileSystem(ctx, fileSystem); }; -export const saveFile = async (ctx, path, fileName, messageId) => { +const saveFile = async (ctx, path, fileName, messageId) => { const fileSystem = await getFileSystem(ctx); const targetDirectory = getDirectory(fileSystem, path); for (const file in targetDirectory['.']){ @@ -203,7 +203,7 @@ export const saveFile = async (ctx, path, fileName, messageId) => { await storeFileSystem(ctx, fileSystem); }; -export const deleteDirectory = async (ctx, path, directoryName) => { +const deleteDirectory = async (ctx, path, directoryName) => { const fileSystem = await getFileSystem(ctx); const targetDirectory = getDirectory(fileSystem, path); const directoryContent = targetDirectory[directoryName]; @@ -213,7 +213,7 @@ export const deleteDirectory = async (ctx, path, directoryName) => { await storeFileSystem(ctx, fileSystem); }; -export const deleteFile = async (ctx, path, fileName) => { +const deleteFile = async (ctx, path, fileName) => { const fileSystem = await getFileSystem(ctx); const targetDirectory = getDirectory(fileSystem, path); const fileContent = targetDirectory['.'].find(f => f.name === fileName); @@ -224,7 +224,7 @@ export const deleteFile = async (ctx, path, fileName) => { await storeFileSystem(ctx, fileSystem); }; -export const renameFile = async (ctx, path, oldFileName, newFilename) => { +const renameFile = async (ctx, path, oldFileName, newFilename) => { const fileExtension = oldFileName.split('.').pop() const fileSystem = await getFileSystem(ctx); const targetDirectory = getDirectory(fileSystem, path); @@ -234,7 +234,7 @@ export const renameFile = async (ctx, path, oldFileName, newFilename) => { await storeFileSystem(ctx, fileSystem); }; -export const moveFile = async (ctx, oldPath, newPath, fileName) => { +const moveFile = async (ctx, oldPath, newPath, fileName) => { const fileSystem = await getFileSystem(ctx); const sourceDirectory = getDirectory(fileSystem, oldPath); const targetDirectory = getDirectory(fileSystem, newPath); @@ -242,4 +242,19 @@ export const moveFile = async (ctx, oldPath, newPath, fileName) => { targetDirectory['.'].push(fileContent); sourceDirectory['.'] = sourceDirectory['.'].filter(f => f.name !== fileName); await storeFileSystem(ctx, fileSystem); +}; + +module.exports = { + storeFileSystem, + initializeFileSystem, + getFileSystem, + getElementsInPath, + getKeyboardDirectories, + getParentDirectoryPath, + mkdir, + saveFile, + deleteDirectory, + deleteFile, + renameFile, + moveFile, }; \ No newline at end of file diff --git a/src/helpers.js b/src/helpers.js index 5379304..c6b6d13 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,12 +1,12 @@ -export const getCurrentPath = (ctx) => { +module.exports.getCurrentPath = (ctx) => { return ctx.session.currentPath ? ctx.session.currentPath : '/'; }; -export const setCurrentPath = (ctx, path) => { +module.exports.setCurrentPath = (ctx, path) => { ctx.session.currentPath = path; }; -export const getFileExtension = async (ctx, fileType) => { +module.exports.getFileExtension = async (ctx, fileType) => { let fileId, fileInfo, extension; switch (fileType) { case 'photo':