Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates related to file streaming #17

Merged
merged 2 commits into from
May 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions gok/gok.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,53 @@ let getFileData = (req, res) => {
message : undefined
}

let filePath = req.query.path;
const filePath = req.query.path;
const range = req.headers.range;

filesdk.getInfo(filePath)
.then((stats) => {
if(!stats.isDirectory()) {
try {
let extension = path.extname(filePath);

res.writeHead(200, {
'Content-Type': mapContentType[extension] || 'text/plain',
'Content-Disposition': 'inline; filename=\"' + path.basename(filePath) + '\"'
});
.then(stats => {
if(stats.isDirectory()) {
throw filePath + ' is a directory.';
} else {
const basename = path.basename(filePath);
const extension = path.extname(filePath);
const fileSize = stats.size;

if(range) {
const parts = range.replace(/bytes=/, '').split('-');

const start = parseInt(parts[0], 10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize - 1;
const chunkSize = (end - start) + 1;

const file = fs.createReadStream(filePath, {start, end});

const head = {
'Content-Type' : mapContentType[extension] || 'text/plain',
'Content-Range' : `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges' : 'bytes',
'Content-Disposition' : `inline; filename=${path.basename(filePath)}"`,
'Content-Length' : chunkSize
};

res.writeHead(206, head);
file.pipe(res);

} else {
const head = {
'Content-Type' : mapContentType[extension] || 'text/plain',
'Content-Disposition' : `inline; filename=${basename}"`,
'Content-Length' : fileSize
};

res.writeHead(200, head);
fs.createReadStream(filePath).pipe(res);
}
catch(error) {
throw error;
}
} else {
throw filePath + ' is a directory.'
}
})
.catch((error) => {
console.log(error);

response = { ...response, message : error };
res.status(200).json(response);
});
Expand Down
19 changes: 12 additions & 7 deletions ui/src/components/ContentArea.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { Component } from 'react';
import { Icon, Card, Divider, Header, Modal } from 'semantic-ui-react';

import { Player } from 'video-react';
import PDFViewer from 'mgr-pdf-viewer-react';

import _ from 'lodash';
Expand Down Expand Up @@ -34,6 +33,16 @@ const mapIcon = {
'dir' : ['folder outline', 'yellow']
}

const VideoPlayer = (props) => {
return (
<video
controls
autoPlay
src={props.src}
/>
)
}

class ContentArea extends Component {

constructor(props) {
Expand Down Expand Up @@ -75,11 +84,7 @@ class ContentArea extends Component {
case '.wav':
case '.mp4':
fileViewer = (
<Player
autoPlay
playsInline
src={url}
/>
<VideoPlayer src={url} />
);
break;
case '.pdf':
Expand Down Expand Up @@ -114,7 +119,7 @@ class ContentArea extends Component {
closeOnEscape={true}
closeIcon
>
<Modal.Content>
<Modal.Content style={{ textAlign : 'center' }}>
{ this.getFileViewer() }
</Modal.Content>
</Modal>
Expand Down