Skip to content

Commit

Permalink
feat: commonjs module
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed Jul 23, 2021
1 parent a30b86c commit 78db2d2
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 61 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -421,4 +421,7 @@ bot.on('text', async (ctx) => {
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));

export default bot;
module.exports.setBotToken = (token) => {
bot.telegram.token = token;
};
module.exports.infiniteCloudBot = bot;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"author": "Luca8991",
"license": "MIT",
"private": false,
"type": "module",
"dependencies": {
"node-fetch": "^2.6.1",
"telegraf": "^4.3.0",
Expand Down
109 changes: 75 additions & 34 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -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);
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,
};
49 changes: 32 additions & 17 deletions src/filesystem.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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));
Expand All @@ -62,7 +62,7 @@ export const storeFileSystem = async (ctx, fileSystem) => {
});
};

export const initializeFileSystem = async (ctx) => {
const initializeFileSystem = async (ctx) => {
await storeFileSystem(ctx, FILESYSTEM_INIT);
};

Expand All @@ -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();
Expand All @@ -98,7 +98,7 @@ const getDirectory = (fileSystem, path) => {
return currentDirectory;
};

export const getElementsInPath = (
const getElementsInPath = (
fileSystem,
path,
hideCurrentDirectory,
Expand Down Expand Up @@ -138,7 +138,7 @@ export const getElementsInPath = (
return elements;
};

export const getKeyboardDirectories = async (
const getKeyboardDirectories = async (
ctx,
currentPath,
hideCurrentDirectory = false,
Expand Down Expand Up @@ -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) {
Expand All @@ -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['.']){
Expand All @@ -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];
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -234,12 +234,27 @@ 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);
const fileContent = sourceDirectory['.'].find(f => f.name === 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,
};
6 changes: 3 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -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':
Expand Down

0 comments on commit 78db2d2

Please sign in to comment.