Skip to content

Commit

Permalink
Web part: refacto + progressive upload with id
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lando committed Oct 26, 2023
1 parent b90c2dc commit f82aa96
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 212 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,6 @@ dependencies:
video_uploader: ^1.1.0
```
### For web usage
Add the api.video TypeScript uploader script to your `web/index.html` head.

```html
<!DOCTYPE html>
<html>
<head>
...
<script src="https://unpkg.com/@api.video/video-uploader" defer></script>
</head>
<body>
...
</body>
</html>
```

## Android
### Permissions
Expand Down
1 change: 0 additions & 1 deletion example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
</script>
<!-- This script adds the flutter initialization JS code -->
<script src="flutter.js" defer></script>
<script src="https://unpkg.com/@api.video/video-uploader" defer></script>
</head>
<body>
<script>
Expand Down
144 changes: 144 additions & 0 deletions lib/assets/uploader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@

function setApplicationName(name, version) {
window.apiVideoFlutterUploader.params.application = {
name,
version
}
}

function setChunkSize(chunkSize) {
window.apiVideoFlutterUploader.params.chunkSize = chunkSize;
}

function getProgressiveSession(sessionId) {
return window.apiVideoFlutterUploader.progressiveSessions[sessionId];
}

function setProgressiveSession(sessionId, progressiveSession) {
window.apiVideoFlutterUploader.progressiveSessions = window.apiVideoFlutterUploader.progressiveSessions || {};
window.apiVideoFlutterUploader.progressiveSessions[sessionId] = progressiveSession;
}

function createProgressiveUploadWithUploadTokenSession(sessionId, uploadToken, videoId) {
return progressiveUploadCreationHelper({
sessionId,
uploadToken,
videoId: videoId || undefined,
});
}

function createProgressiveUploadWithApiKeySession(sessionId, apiKey, videoId) {
return progressiveUploadCreationHelper({
sessionId,
apiKey,
videoId: videoId || undefined,
});
}

async function uploadPart(sessionId, filePath, onProgress) {
return await uploadPartHelper(sessionId, filePath, onProgress, async (session, blob) => {
return await session.uploader.uploadPart(blob);
});
}

async function uploadLastPart(sessionId, filePath, onProgress) {
return await uploadPartHelper(sessionId, filePath, onProgress, async (session, blob) => {
return await session.uploader.uploadLastPart(blob);
});
}

async function getBlobFromPath(filePath) {
return await fetch(filePath)
.then(r => r.blob());
}


async function uploadWithUploadToken(filePath, uploadToken, videoName, onProgress, videoId) {
return uploadHelper(filePath, onProgress, { uploadToken, videoName, videoId });
}

async function uploadWithApiKey(filePath, apiKey, onProgress, videoId) {
return uploadHelper(filePath, onProgress, { apiKey, videoId });
}


function progressiveUploadCreationHelper(options) {
const uploader = new ProgressiveUploader({
...options,
origin: getOriginHeader(),
});

uploader.onProgress((e) => {
const onProgress = getProgressiveSession(options.sessionId).partsOnProgress[e.part];
if (onProgress != null) {
onProgress(e.uploadedBytes / e.totalBytes);
}
});

setProgressiveSession(options.sessionId, {
uploader,
partsOnProgress: {},
currentPart: 1,
});

return Promise.resolve();
}

async function uploadPartHelper(sessionId, filePath, onProgress, uploadCallback) {
const blob = await getBlobFromPath(filePath);
const session = getProgressiveSession(sessionId);

if (onProgress != null) {
session.partsOnProgress[session.currentPart] = onProgress;
}

session.currentPart++;

try {
return JSON.stringify(await uploadCallback(session, blob));
} catch (e) {
throw new Error(e.title);
}
}

async function uploadHelper(filePath, onProgress, options) {
const blob = await getBlobFromPath(filePath);

const uploader = new VideoUploader({
file: blob,
chunkSize: 1024 * 1024 * window.apiVideoFlutterUploader.params.chunkSize,
origin: getOriginHeader(),
...options
});

if (onProgress != null) {
uploader.onProgress((e) => onProgress(e.uploadedBytes / e.totalBytes));
}
try {
return JSON.stringify(await uploader.upload());
} catch (e) {
throw new Error(e.title);
}

}

function getOriginHeader() {
return {
sdk: {
name: 'flutter-uploader',
version: window.apiVideoFlutterUploader.params.sdkVersion,
},
application: window.apiVideoFlutterUploader.params.application
};
}



// https://github.com/flutter/flutter/issues/126713
function fixRequireJs() {
if (typeof window.define == 'function') {
delete window.define.amd;
delete window.exports;
delete window.module;
}
}
Loading

0 comments on commit f82aa96

Please sign in to comment.