Question about installing modules

Hi
this is a new thing to me. but is it possible to use this on the server?

i am in need to split videos and sadly its not possible on client mobile and i was advised to use a server solution.

can you guys guide me in this matter please…
Thanks you

Hello @mohammad_altoiher

Actually, we are not responsible for any 3rd party libraries/modules, however, seems like it should work on the server. You need to create/generate a BL project and deploy it on our servers.

https://backendless.com/docs/bl-js/bl_coderunner.html

At this moment our JSCodeRunner is running under NodeJS v12.x, and very soon we are going to upgrade it v14.x.

Regards, Vlad

the module is installed on the server thanks Vlad

how can i instruct the JS server code to export a file to Files?

'use strict';

class SplitVideo {
  /**
   * @param {String} name
   * @returns {String}
   */
  SplitVideoMethod(name) {
	  
	let MediaSplit = require('../node_modules/media-split');
	let split = new MediaSplit({ 
	input: 'https://www.youtube.com/watch?v=qbRqySW39TI&ab_channel=RoCarsRoCars', 
	sections: ['[00:01 - 00:03] Myfile'],
	format: 'mp4',
	output: ''// <------ Here i should point to where to save the file
	 });
		split.parse().then((sections) => {
		  for (let section of sections) {
			console.log(section.name);      // filename
			console.log(section.start);     // section start
			console.log(section.end);       // section end
			console.log(section.trackName); // track name
		  }
		});
	}
}

SplitVideo.version = '1.0.0';

Backendless.ServerCode.addService(SplitVideo);

I would recommend you to write a file on the disk using basic NodeJS API since you’ve got full access to your Files Storage from the Business Logic

The output is a file path.
how can i redirect it to my backendless files?

here is a topic about file path node.js - NodeJS accessing file with relative path - Stack Overflow

So its not a fixed bath that i can point to like the REST API?

I’m not sure I understand your question, could you please clarify?

I’m not familiar with the MediaSplit module and but I assume the output property should be a path to the file (not URL)

Yes correct the file path
so lets say i want to point to the root of my files.
what should the value be?

it depends on where your business logic file is located, for instance my API Service is located here

now, let’s create a test method to see where we are and calculate path to our root folder:


 getPath(){
   const fs = require('fs');
   const path = require('path');

    return {
      currentDirectoryPath: __dirname,
      myAppRootDirectoryPath: path.resolve(__dirname, '../../../../../')
    }
  }

running the method we can see the following result:

{
    "currentDirectoryPath": "/opt/backendless/repo/MY_APP_ID/files/servercode/JS/default/PRODUCTION/services",
    "myAppRootDirectoryPath": "/opt/backendless/repo/MY_APP_ID/files"
}

make sure you do not hardcode this path /opt/backendless/repo/... because this is an internal path and it might be changed without any notice.

I just created an internal ticket BKNDLSS-25505 to discuss with the team a more simple way for retrieving the filesRootPath

1 Like

Hey
following you advice i was able to tested debug the method locally successfully but when i deploy there is no output and there is no errors in the log file.
is there anything i can do to debug the issue?

here is the code that work locally and with remote debug successfully

'use strict';

class SplitVideo {
  /**
   * @param {String} name
   * @returns {String}
   */
 async SplitVideoMethod(name) {
	  
	let MediaSplit = require('../node_modules/media-split');
	
   const fs = require('fs');
   const path = require('path');


      let currentDirectoryPath =  __dirname;
      let myAppRootDirectoryPath = path.resolve(__dirname, '../../../../../');
      Backendless.Logging.setLogReportingPolicy( 1, 1 );
      var logger = Backendless.Logging.getLogger( "MyLogger");
      logger.info( myAppRootDirectoryPath ); 
     
	let split = new MediaSplit({ 
	input: myAppRootDirectoryPath+'/ads_service/0391E4B39-D876-40A3-A48F-E42A74B05770.mp4', 
	sections: ['[00:01 - 00:03] Myfile'],
	format: 'mp4',
	output:myAppRootDirectoryPath
	 });
		split.parse().then((sections) => {
		  for (let section of sections) {
         logger.info( 'File Exported' ); 
		  }
		});
	}
}

SplitVideo.version = '1.0.0';

Backendless.ServerCode.addService(SplitVideo);

This is the log when i invoke the service:

2021-06-10 20:11:52,402 | SERVER_CODE |  INFO | [16609] ServerCode Model built in 7ms
2021-06-10 20:11:52,408 | SERVER_CODE |  INFO | [16609] [347FBCFF-B1DE-1B48-FFF4-5CCE31AEE100] [INVOKE SERVICE] services.SplitVideo.SplitVideoMethod
2021-06-10 20:11:52,484 | MyLogger |  INFO | /opt/backendless/repo/07d0cc0c-4a50-198b-fffb-78866ccd9600/files
2021-06-10 20:11:52,489 | SERVER_CODE |  INFO | [16609] Processing finished in 102.807ms

in case you need my app id: 07D0CC0C-4A50-198B-FFFB-78866CCD9600

try to put an await in front of split.parse()

we are getting somewhere!

400 - Output path "/opt/backendless/repo/07d0cc0c-4a50-198b-fffb-78866ccd9600/files" is not writable

ServerCodeUser have the right to read and write to all the files

output:myAppRootDirectoryPath - is a directory, have you tried to specify a path to a file?

the module create the files in the output directory (this is what it did locally when testing)

i tried to change it to a sub folder in the files, and the same error occur.

if i put a random folder name that doesn’t exist. another error about the directory not found display.

I’m so sorry but seems like I was pointing you the wrong way.
There is no way to write directly on the File Storage using Nodejs API, the only way is to use Backendless.Files API.

So does this mean i cant make it work :worried:

if this library can return a ReadStream you can use the following method:

 Backendless.Files.upload(readStream: ReadStream, path: string, overwrite?: boolean): Promise<Object>;

No it cant. it saves a file to path… simple

is there a reason why we cant create a file on the server from a script that is running on the server? cant you guys add this feature?
its super useful and adds a lot of possibility to backendless