Skip to main content

Overview

The File Manager provides two complementary classes for file operations in Loopar Framework:
  1. FileManage - Server-side file system operations (create, delete, import)
  2. FileManager - File type detection, icon mapping, and media handling

Import

import { fileManage, fileManager } from 'loopar';

FileManage Class

Server-side file system operations for creating files, folders, and managing configuration.

File Operations

makeFile(destiny, name, content, ext, replace)

Creates a file at the specified location.
destiny
string
required
Destination directory path (relative to pathRoot)
name
string
required
File name (without extension)
content
string
required
File content to write
ext
string
default:"js"
File extension
replace
boolean
default:"false"
Whether to replace existing file
await fileManage.makeFile(
  'apps/my-app/controllers',
  'my-controller',
  'export default class MyController {}',
  'js',
  false
);

existFile(fileRoute)

Checks if a file exists asynchronously.
fileRoute
string
required
File path (relative to pathRoot or absolute with ’./‘)
const exists = await fileManage.existFile('apps/my-app/config.json');
if (exists) {
  console.log('File exists');
}

existFileSync(fileRoute)

Checks if a file exists synchronously.
fileRoute
string
required
File path
if (fileManage.existFileSync('./package.json')) {
  console.log('Package.json found');
}

existFolder(folderRoute)

Checks if a folder exists.
folderRoute
string
required
Folder path (relative to pathRoot)
const exists = await fileManage.existFolder('apps/my-app');

Folder Operations

makeFolder(…args)

Creates a folder with recursive directory creation.
args
...string
required
Path segments to create
await fileManage.makeFolder('apps', 'my-app', 'modules', 'core');
// Creates: {pathRoot}/apps/my-app/modules/core

deleteFolder(destiny, name)

Deletes a folder recursively.
destiny
string
required
Parent directory path
name
string
required
Folder name to delete
await fileManage.deleteFolder('apps', 'old-app');

Class Generation

makeClass(destiny, name, options, importerType, ext)

Generates a JavaScript/TypeScript class file.
destiny
string
required
Destination directory
name
string
required
Class name
options
object
Class generation options
options.IMPORTS
object
Import statements (key: name, value: path)
options.EXTENDS
string
Parent class name
importerType
string
default:""
‘default’ or ” for named imports
ext
string
default:"js"
File extension
await fileManage.makeClass(
  'apps/my-app/controllers',
  'MyController',
  {
    IMPORTS: {
      BaseController: 'loopar',
      loopar: 'loopar'
    },
    EXTENDS: 'BaseController'
  },
  '',
  'js'
);

// Generates:
// 'use strict';
// 
// import {BaseController} from 'loopar';
// import {loopar} from 'loopar';
// 
// export default class MyController extends BaseController {
//     constructor(props){
//         super(props);
//     }
// }

Module Import

importClass(fileRoute, onError)

Dynamically imports a JavaScript class.
fileRoute
string
required
Path to file to import
onError
function|object
Error handler (function, class, or object)
try {
  const MyClass = await fileManage.importClass(
    'apps/my-app/controllers/my-controller.js',
    (error) => {
      console.error('Failed to import:', error);
      return DefaultController;
    }
  );
  
  const instance = new MyClass();
} catch (error) {
  console.error('Import error:', error);
}

Configuration Files

getConfigFile(fileName, path)

Reads and parses a JSON configuration file.
fileName
string
required
Configuration file name (without .json extension)
path
string
Custom path (defaults to sites//config)
const dbConfig = fileManage.getConfigFile('db.config');
console.log(dbConfig.host, dbConfig.database);

const appConfig = fileManage.getConfigFile(
  'installer',
  'apps/my-app'
);

setConfigFile(fileName, data, path)

Writes data to a JSON configuration file.
fileName
string
required
Configuration file name
data
object
required
Data to write (will be JSON stringified)
path
string
Custom path
await fileManage.setConfigFile('db.config', {
  host: 'localhost',
  port: 3306,
  database: 'my_db',
  username: 'root',
  password: 'secret'
});

await fileManage.setConfigFile(
  'settings',
  { theme: 'dark', language: 'en' },
  'apps/my-app/config'
);

getAppData(appName)

Retrieves installer configuration for an app.
appName
string
required
Application name
const appData = fileManage.getAppData('my-app');
console.log(appData.version, appData.dependencies);

Naming Utilities

fileName(name, ext)

Converts name to kebab-case filename with extension.
name
string
required
Name to convert
ext
string
default:"js"
File extension
fileManage.fileName('MyController', 'js');
// Returns: 'my-controller.js'

fileManage.fileName('UserProfile', 'json');
// Returns: 'user-profile.json'

folderName(name)

Converts name to kebab-case folder name.
fileManage.folderName('MyApp');
// Returns: 'my-app'

className(name)

Removes spaces from name for class naming.
fileManage.className('My Controller');
// Returns: 'MyController'

FileManager Class

Client and server-side file type detection, icon mapping, and media processing.

File Type Detection

getFileType(file)

Detects file type from file object or name.
file
object|string
required
File object or filename
const type = fileManager.getFileType({ 
  name: 'document.pdf',
  type: 'application/pdf'
});
// Returns: 'pdf'

const type2 = fileManager.getFileType({
  name: 'photo.jpg'
});
// Returns: 'image'
Detected Types:
  • image: JPG, PNG, GIF, SVG, WebP, etc.
  • video: MP4, AVI, WebM, MOV, etc.
  • audio: MP3, WAV, OGG, etc.
  • pdf: PDF files
  • word: DOC, DOCX
  • excel: XLS, XLSX
  • powerpoint: PPT, PPTX
  • zip: ZIP, RAR, 7Z
  • code: JS, HTML, CSS, Python, etc.
  • text: TXT, MD
  • folder: Directories
  • default: Unknown types

getTypeByExtension(ext)

Returns file type for a given extension.
ext
string
required
File extension (without dot)
fileManager.getTypeByExtension('jpg');  // 'image'
fileManager.getTypeByExtension('mp4');  // 'video'
fileManager.getTypeByExtension('docx'); // 'word'
fileManager.getTypeByExtension('xyz');  // 'default'

getTypeFromURL(url)

Detects file type from URL patterns.
url
string
required
File URL
fileManager.getTypeFromURL('https://example.com/photo.jpg');
// Returns: 'image'

fileManager.getTypeFromURL('https://res.cloudinary.com/demo/image/upload/sample.jpg');
// Returns: 'image'

getExtention(file)

Extracts file extension from filename or URL.
file
string
required
Filename or URL
getExtention('document.pdf');     // 'pdf'
getExtention('photo.JPG');        // 'jpg' (lowercase)
getExtention('https://example.com/file.mp4'); // 'mp4'

Icon Mapping

getIconByExtention(ext, type)

Returns icon configuration for file type.
ext
string
required
File extension
type
string
File type (folder, image, etc.)
const icon = fileManager.getIconByExtention('pdf', 'pdf');
// Returns: { icon: 'fas fa-file-pdf', color: 'danger' }

const folderIcon = fileManager.getIconByExtention('', 'folder');
// Returns: { icon: 'fas fa-folder', color: 'yellow' }

getFileIcon(type)

Returns icon class for file type.
type
string
required
File type
fileManager.getFileIcon('image');  // 'fas fa-file-image'
fileManager.getFileIcon('video');  // 'fas fa-file-video'
fileManager.getFileIcon('pdf');    // 'fas fa-file-pdf'

File Processing

getMappedFiles(files)

Processes and normalizes file data for display.
files
array|string|object
required
Files to process (various formats supported)
// From file input
const files = fileManager.getMappedFiles([
  { name: 'photo.jpg', size: 1024000 },
  { name: 'document.pdf', size: 2048000 }
]);

// Returns:
[
  {
    name: 'photo.jpg',
    type: 'image',
    src: '/assets/public/images/photo.jpg',
    extention: 'jpg',
    previewSrc: '/assets/public/images/thumbnails/photo.jpg'
  },
  {
    name: 'document.pdf',
    type: 'pdf',
    src: '/assets/public/images/document.pdf',
    extention: 'pdf',
    previewSrc: '/assets/public/images/document.pdf'
  }
]

// From URL string
const urlFiles = fileManager.getMappedFiles('https://example.com/photo.jpg');

// From JSON string
const jsonFiles = fileManager.getMappedFiles(
  '[{"name":"file.jpg","size":1024}]'
);

getSrc(file, preview, ext)

Generates source URL for a file.
file
object
required
File object
preview
boolean
default:"false"
Whether to get thumbnail URL
ext
string
File extension
const src = fileManager.getSrc(
  { name: 'photo.jpg', src: null },
  false
);
// Returns: '/assets/public/images/photo.jpg'

const thumbnail = fileManager.getSrc(
  { name: 'photo.jpg', src: null },
  true,
  'jpg'
);
// Returns: '/assets/public/images/thumbnails/photo.jpg'

// SVG and ICO don't get thumbnails
const icon = fileManager.getSrc(
  { name: 'icon.svg' },
  true,
  'svg'
);
// Returns: '/assets/public/images/icon.svg' (no thumbnail path)

getImage(data, field, avatar)

Extracts image URL from document data.
data
object
required
Document data object
field
string
required
Field name containing image
avatar
string
Default avatar name if no image
const profilePic = fileManager.getImage(
  { profile_picture: '[{"name":"avatar.jpg"}]' },
  'profile_picture'
);
// Returns: '/assets/public/images/thumbnails/avatar.jpg'

const defaultAvatar = fileManager.getImage(
  { profile_picture: null },
  'profile_picture',
  'default-male.jpg'
);
// Returns: '/assets/public/images/avatars/default-male.jpg'

getFileSize(bytes, decimals)

Formats file size in human-readable format.
bytes
number
required
File size in bytes
decimals
number
default:"2"
Decimal places
const [size, unit] = fileManager.getFileSize(1024);
// Returns: [1.00, 'KB']

const [size2, unit2] = fileManager.getFileSize(1048576);
// Returns: [1.00, 'MB']

const [size3, unit3] = fileManager.getFileSize(2500000, 1);
// Returns: [2.4, 'MB']

Utility Functions

isURL(str)

Checks if string is a valid URL.
str
string
required
String to check
fileManager.isURL('https://example.com/file.jpg');  // true
fileManager.isURL('example.com');                    // false
fileManager.isURL('file.jpg');                       // false

Complete Example

import { fileManage, fileManager } from 'loopar';
import { loopar } from 'loopar';

// Create application structure
await fileManage.makeFolder('apps', 'my-app', 'modules', 'core', 'controllers');

// Generate controller class
await fileManage.makeClass(
  'apps/my-app/modules/core/controllers',
  'MyController',
  {
    IMPORTS: { BaseController: 'loopar' },
    EXTENDS: 'BaseController'
  }
);

// Save configuration
await fileManage.setConfigFile('app-settings', {
  name: 'My App',
  version: '1.0.0'
});

// Check if file exists
if (await fileManage.existFile('apps/my-app/config.json')) {
  const config = fileManage.getConfigFile('config', 'apps/my-app');
  console.log('App config:', config);
}

// Process uploaded files
const uploadedFiles = fileManager.getMappedFiles(request.files);

for (const file of uploadedFiles) {
  console.log(`File: ${file.name}`);
  console.log(`Type: ${file.type}`);
  console.log(`Icon: ${fileManager.getFileIcon(file.type)}`);
  console.log(`Size: ${fileManager.getFileSize(file.size).join(' ')}`);
}

// Get user profile image
const user = await loopar.getDocument('User', '[email protected]');
const userData = await user.values();
const profileImage = fileManager.getImage(
  userData,
  'profile_picture',
  'default-avatar.jpg'
);

console.log('Profile image URL:', profileImage);

File Type Extensions Reference

jpg, jpeg, png, gif, svg, bmp, ico, webp, tiff, tif, psd, ai, raw, indd, heif, heic, eps, svgz, jfif
mp4, avi, mkv, webm, mov, flv, wmv, mpg, mpeg, 3gp, 3g2, m4v, h264, rmvb, vob, ts, m2ts, mts, divx, xvid
mp3, wav, ogg, m4a, wma, flac, aac, aiff, alac, pcm, dsd, awb, ac3, dts, mp2, mka, opus, ra, rm
  • PDF: pdf, ps
  • Word: doc, docx
  • Excel: xls, xlsx
  • PowerPoint: ppt, pptx
  • Text: txt, md
html, css, js, php, py, java, cpp, c, cs, go, ts, tsx, jsx, rb, sh, bat, cmd
zip, rar, 7z, tar

Build docs developers (and LLMs) love