@gsb-core/core
Version:
GSB core services and classes for platform-independent web applications
237 lines • 8.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GsbFileService = exports.DEFAULT_FOLDER_TYPE = void 0;
const gsb_entity_service_1 = require("../../services/entity/gsb-entity.service");
const query_params_1 = require("../../types/query-params");
const query_1 = require("../../types/query");
const gsb_config_1 = require("../../config/gsb-config");
const gsb_file_model_1 = require("../../models/gsb-file.model");
const gsb_utils_1 = require("../../utils/gsb-utils");
exports.DEFAULT_FOLDER_TYPE = gsb_file_model_1.FileType.Cloud_AWS;
/**
* Service for managing Files and Storage in the application
*/
class GsbFileService {
constructor() {
this.ENTITY_NAME = 'GsbFile';
this.entityService = gsb_entity_service_1.GsbEntityService.getInstance();
}
static getInstance() {
if (!GsbFileService.instance) {
GsbFileService.instance = new GsbFileService();
}
return GsbFileService.instance;
}
/**
* Get file by ID
* @param id The file ID
* @returns The file or null if not found
*/
async getFileById(id) {
try {
const file = await this.entityService.getById(this.ENTITY_NAME, id, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return file;
}
catch (error) {
console.error('Error getting file by ID:', error);
return null;
}
}
/**
* Get files in a folder
* @param folderId The parent folder ID (null or undefined for root)
* @param page The page number (starting from 1)
* @param pageSize The number of items per page
* @returns An array of files and the total count
*/
async getFiles(folderId, page = 1, pageSize = 10) {
try {
const query = new query_params_1.QueryParams(this.ENTITY_NAME);
// Filter by parent folder
if (folderId) {
query.filters = [
{
propVal: {
name: 'parent_id',
value: folderId
},
function: query_1.QueryFunction.Equals
}
];
}
else {
// For root, parent_id should be null or undefined
query.filters = [
{
propVal: {
name: 'parent_id',
value: null
},
function: query_1.QueryFunction.Is
}
];
}
// Set pagination
query.startIndex = (page - 1) * pageSize;
query.count = pageSize;
query.calcTotalCount = true;
// Sort by creation date
query.sortCols = (0, gsb_utils_1.getGsbDateSortCols)();
const response = await this.entityService.query(query, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return {
files: (response.entities || []),
totalCount: response.totalCount || 0
};
}
catch (error) {
console.error('Error getting files:', error);
return { files: [], totalCount: 0 };
}
}
/**
* Search for files
* @param searchTerm The search term
* @param page The page number (starting from 1)
* @param pageSize The number of items per page
* @returns An array of files and the total count
*/
async searchFiles(searchTerm, page = 1, pageSize = 10) {
try {
const query = new query_params_1.QueryParams(this.ENTITY_NAME);
// Set search filter
if (searchTerm) {
query.searchText = searchTerm;
}
// Set pagination
query.startIndex = (page - 1) * pageSize;
query.count = pageSize;
query.calcTotalCount = true;
const response = await this.entityService.query(query, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return {
files: (response.entities || []),
totalCount: response.totalCount || 0
};
}
catch (error) {
console.error('Error searching files:', error);
return { files: [], totalCount: 0 };
}
}
/**
* Create a new folder
* @param name The folder name
* @param parentId The parent folder ID (optional)
* @returns The created folder ID or null if failed
*/
async createFolder(name, parentId, parentPath) {
try {
// Set defaults
const newFolder = {
name,
fileType: exports.DEFAULT_FOLDER_TYPE,
listingType: gsb_file_model_1.ListingType.Folder,
parent_id: parentId || '',
path: parentPath ? `${parentPath}/${name}` : undefined
};
// Set create date fields
const request = {
entDefName: this.ENTITY_NAME,
entity: newFolder,
entityDef: {},
entityId: '',
entDefId: '',
filters: []
};
const response = await this.entityService.save(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
if (!response.id) {
return null;
}
return response.id;
}
catch (error) {
console.error('Error creating folder:', error);
return null;
}
}
/**
* Upload a file
* @param file The file to upload
* @param name The file name (optional, defaults to file.name)
* @param parentId The parent folder ID (optional)
* @returns The uploaded file ID or null if failed
*/
async uploadFile(file, entity) {
try {
// Create file metadata
const newFile = {
name: entity.name || file.name,
fileType: exports.DEFAULT_FOLDER_TYPE,
listingType: gsb_file_model_1.ListingType.File,
parent_id: entity.parent_id,
size: file.size,
contentType: file.type,
path: entity.path
};
const request = {
file: file,
entity: newFile
};
const response = await this.entityService.uploadFile(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return (response === null || response === void 0 ? void 0 : response.fileDto) || null;
}
catch (error) {
console.error('Error uploading file:', error);
return null;
}
}
/**
* Delete a file or folder
* @param id The file or folder ID to delete
* @returns True if deleted successfully, false otherwise
*/
async deleteFile(id) {
try {
const request = {
entDefName: this.ENTITY_NAME,
entityId: id
};
const response = await this.entityService.delete(request, (0, gsb_config_1.getGsbToken)(), (0, gsb_config_1.getGsbTenantCode)());
return response.deleteCount === 1;
}
catch (error) {
console.error('Error deleting file:', error);
return false;
}
}
/**
* Get file content
* @param id The file ID
* @returns The file content or null if failed
*/
async getFileContent(id) {
try {
const file = await this.getFileById(id);
if (!file) {
throw new Error('File not found');
}
if (file.listingType !== gsb_file_model_1.ListingType.File) {
throw new Error('Not a file');
}
if (!file.publicUrl) {
throw new Error('File has no public URL');
}
// Fetch the file content from the public URL
const response = await fetch(file.publicUrl);
if (!response.ok) {
throw new Error(`Failed to fetch file content: ${response.statusText}`);
}
return await response.blob();
}
catch (error) {
console.error('Error getting file content:', error);
return null;
}
}
}
exports.GsbFileService = GsbFileService;
//# sourceMappingURL=file.service.js.map