Skip to content

Commit

Permalink
feat: added isVideoSuitableForUpload method
Browse files Browse the repository at this point in the history
  • Loading branch information
madkarmaa committed Feb 1, 2024
1 parent 70543bc commit 25795dd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"mime-types": "^2.1.35"
},
"devDependencies": {
"jsdoc": "^4.0.2",
"jsdoc-to-markdown": "^7.1.0"
}
}
39 changes: 34 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ axios.interceptors.response.use(
* Client to interact with the `streamable.com` API
*
* `.login()` or `.createAccount()` methods **must** be called before performing any other operations!
*
* @class
*/
class StreamableClient {
#loggedIn = false;
Expand Down Expand Up @@ -71,6 +73,7 @@ class StreamableClient {

/**
* Check if the user is logged in
*
* @returns {Promise<boolean>}
*/
async isLoggedIn() {
Expand All @@ -79,6 +82,7 @@ class StreamableClient {

/**
* Get the currently logged in user's data
*
* @returns {Promise<object>} The user's data
*/
async getUserData() {
Expand All @@ -88,6 +92,7 @@ class StreamableClient {

/**
* Get the current user's plan's data
*
* @returns {Promise<object>} The user's current plan's data
*/
async getPlanData() {
Expand All @@ -97,6 +102,7 @@ class StreamableClient {

/**
* Get the current user's videos data
*
* @returns {Promise<object[]>} The current user's videos data
*/
async getAllVideosData() {
Expand All @@ -114,12 +120,9 @@ class StreamableClient {
return (await axios.get(endpoints.VIDEO(shortcode), { headers: this.#headers })).data;
}

async #willReachUploadLimits(video_size) {
if (!(await this.isLoggedIn())) return console.error('You must be logged in to use this method!');
}

/**
* Upload a video from a given url
*
* @param {URL | String} url The url of the video file to upload
* @returns {Promise<object>} The uploaded video's data
*/
Expand Down Expand Up @@ -172,6 +175,7 @@ class StreamableClient {

/**
* Delete a video from the user's account
*
* @param {String} shortcode The shortcode of the video
* @returns {Promise<void>}
*/
Expand All @@ -185,6 +189,7 @@ class StreamableClient {

/**
* Delete all videos from the user's account
*
* @returns {Promise<void>}
*/
async deleteAllVideos() {
Expand Down Expand Up @@ -234,13 +239,15 @@ class StreamableClient {

/**
* Upload a local video
*
* @param {String} videoPath The path to the video file
* @returns {Promise<object>} The uploaded video's data
*/
async uploadLocalVideo(videoPath) {
videoPath = path.resolve(videoPath);
const isVideoValid = await this.isVideoSuitableForUpload(videoPath);

if (!lookup(videoPath).startsWith('video')) return console.error('Please provide a video file!');
if (!isVideoValid.isValid) return console.error(isVideoValid.reason);

const { size: videoSize } = fs.statSync(videoPath);

Expand Down Expand Up @@ -311,6 +318,28 @@ class StreamableClient {
)
).data;
}

/**
* Check if a local file is suitable for upload based off the current plan's limitations
*
* @param {String} videoPath The path to the video file
* @return {Promise<{ reason: String, isValid: boolean }>}
*/
async isVideoSuitableForUpload(videoPath) {
videoPath = path.resolve(videoPath);

if (!lookup(videoPath).startsWith('video')) return { reason: 'Not a video', isValid: false };

const { size: videoSize } = fs.statSync(videoPath);
const { plan_max_length, plan_max_size } = await this.getUserData();

if (videoSize > convert(plan_max_size).from('Gb').to('b')) return { reason: 'Video too large', isValid: false };

if ((await getVideoDurationInSeconds(videoPath)) > plan_max_length)
return { reason: 'Video too long', isValid: false };

return { reason: '', isValid: true };
}
}

module.exports = StreamableClient;

0 comments on commit 25795dd

Please sign in to comment.