Skip to content

Commit

Permalink
add createProgressiveUploadSession()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierapivideo committed Apr 2, 2024
1 parent bd2ffaa commit 7ad400a
Showing 1 changed file with 70 additions and 16 deletions.
86 changes: 70 additions & 16 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NativeModules, Platform } from 'react-native';
import type { Environment, Video } from './types';
import { create } from 'domain';

Check failure on line 3 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

'create' is defined but never used

const LINKING_ERROR =
`The package '@api.video/react-native-video-uploader' doesn't seem to be linked. Make sure: \n\n` +
Expand All @@ -25,6 +26,28 @@ const ApiVideoUploader = ApiVideoUploaderModule
}
);

type ProgressiveUploadParams = { videoId: string };
type ProgressiveUploadWithUploadTokenParams = {
token: string;
videoId?: string;
};

function isProgressiveUploadWithUploadTokenParams(
params: ProgressiveUploadWithUploadTokenParams | ProgressiveUploadParams
): params is ProgressiveUploadWithUploadTokenParams {
return (

Check failure on line 38 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `⏎····(params·as·ProgressiveUploadWithUploadTokenParams).token·!==·undefined⏎··)` with `params·as·ProgressiveUploadWithUploadTokenParams).token·!==·undefined`
(params as ProgressiveUploadWithUploadTokenParams).token !== undefined
);
}

function sanitizeVideo(videoJson: string): Video {
const video = JSON.parse(videoJson);
return {
...video,
_public: video.public,
};
}

export default {
setApplicationName: (name: string, version: string): void => {
ApiVideoUploader.setApplicationName(name, version);
Expand Down Expand Up @@ -54,23 +77,54 @@ export default {
token,
filepath,
videoId
).then((value: string) => {
const json = JSON.parse(value);

return {
...json,
_public: json.public,
} as Video;
});
).then((value: string) => sanitizeVideo(value));
},
upload: (videoId: string, filepath: string): Promise<Video> => {
return ApiVideoUploader.upload(videoId, filepath).then((value: string) => {
const json = JSON.parse(value);

return {
...json,
_public: json.public,
} as Video;
});
return ApiVideoUploader.upload(videoId, filepath).then((value: string) => sanitizeVideo(value));

Check failure on line 83 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `·sanitizeVideo(value)` with `⏎······sanitizeVideo(value)⏎····`
},

createProgressiveUploadSession: (
params: ProgressiveUploadWithUploadTokenParams | ProgressiveUploadParams
) => {
return new (class ProgressiveUploadSession {
private sessionId: string;

constructor() {
this.sessionId = this.generateSessionId();
if (isProgressiveUploadWithUploadTokenParams(params)) {
ApiVideoUploader.createProgressiveUploadWithUploadTokenSession(
this.sessionId,
params.token,
params.videoId
);
} else {
ApiVideoUploader.createProgressiveUploadSession(
this.sessionId,
params.videoId
);
}
}

uploadPart(filepath: string): Promise<Video> {
return ApiVideoUploader.uploadPart(this.sessionId, filepath).then(
(value: string) => sanitizeVideo(value));

Check failure on line 110 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎········`
}

uploadLastPart(filepath: string): Promise<Video> {
return ApiVideoUploader.uploadLastPart(this.sessionId, filepath).then(
(value: string) => sanitizeVideo(value));

Check failure on line 115 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Replace `·=>·sanitizeVideo(value)` with `=>·sanitizeVideo(value)⏎········`
}

dispose() {
ApiVideoUploader.disposeProgressiveUploadSession(this.sessionId);
this.sessionId = this.generateSessionId();
}

private generateSessionId() {
return Math.random().toString(36).substring(2);
}

Check failure on line 126 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
})();
},
};

Check failure on line 130 in src/index.tsx

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`

0 comments on commit 7ad400a

Please sign in to comment.