Skip to content
This repository was archived by the owner on Nov 11, 2024. It is now read-only.

Commit 6bc8288

Browse files
mingwu123123TangZhiZhen
authored andcommitted
Add HTTP upload support for VOD
Signed-off-by: Mingyang Wu <mingyangx.wu@intel.com>
1 parent e2678a5 commit 6bc8288

File tree

10 files changed

+180
-2
lines changed

10 files changed

+180
-2
lines changed

cdn-server/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ RUN mkdir -p /home/build/var/www/tmp/client_body && \
5252
mkdir -p /home/build/var/www/tmp/uwsgi && \
5353
mkdir -p /home/build/var/www/tmp/scgi && \
5454
mkdir -p /home/build/var/www/cache && \
55+
mkdir -p /home/build/var/www/temp && \
5556
mkdir -p /home/build/var/www/html
5657

5758

@@ -62,7 +63,7 @@ WORKDIR /home
6263

6364
# Prerequisites
6465
RUN ln -sf /usr/share/zoneinfo/UTC /etc/localtime && \
65-
DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y -q --no-install-recommends libxml2 libssl-dev libpcre3 zlib1g libxslt1.1 python3-tornado python3-kafka python3-bs4 python3-kazoo vim openssh-server && \
66+
DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y -q --no-install-recommends libxml2 libssl-dev libpcre3 zlib1g libxslt1.1 python3-tornado python3-kafka python3-bs4 python3-kazoo vim openssh-server ffmpeg && \
6667
rm -rf /var/lib/apt/lists/*
6768
# Install
6869
COPY --from=build /home/build /

cdn-server/config.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ timeSpan=20000
44
srcMode=local
55
[path]
66
srcPath=/var/www/archive
7+
tempPath=/var/www/temp
78

cdn-server/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
from tornado.options import define, options, parse_command_line
55
from playlist import PlayListHandler
66
from schedule import ScheduleHandler
7+
from upload import UploadHandler
8+
import os
9+
import configparser
710

811
APP = web.Application([
912
(r'/playlist', PlayListHandler),
1013
(r'/schedule/.*', ScheduleHandler),
14+
(r'/upload/', UploadHandler),
1115
])
1216

17+
config = configparser.ConfigParser()
18+
config.read('config.ini')
19+
tempPath = config.get('path', 'tempPath')
20+
srcPath = config.get('path', 'srcPath')
21+
1322
if __name__ == "__main__":
1423
define("port", default=2222, help="the binding port", type=int)
1524
define("ip", default="127.0.0.1", help="the binding ip")

cdn-server/nginx.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ http {
8989
proxy_pass http://localhost:2222;
9090
}
9191

92+
location /upload/ {
93+
add_header Cache-Control no-cache;
94+
proxy_pass http://localhost:2222;
95+
client_max_body_size 200M;
96+
}
97+
9298
location /hls/ {
9399
root /var/www;
94100
add_header Cache-Control no-cache;

cdn-server/upload.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
import shutil
3+
from tornado.web import RequestHandler
4+
5+
class UploadHandler(RequestHandler):
6+
def post(self, *args, **kwargs):
7+
fileName = self.get_body_argument('fileName', None)
8+
file = self.request.files.get('file', None)
9+
uploadStatus = self.get_body_argument('uploadStatus', None)
10+
timeStamp = self.get_body_argument('timeStamp', None)
11+
from main import tempPath,srcPath
12+
fileName = timeStamp + "-" + fileName
13+
proPath = os.path.join(tempPath, fileName)
14+
try:
15+
with open(proPath, 'ab') as f:
16+
f.write(file[0]['body'])
17+
self.set_status(200)
18+
if uploadStatus == 'end':
19+
shutil.move(proPath, srcPath)
20+
cmd = "ffmpeg -i " + srcPath + "/" + fileName + " -vf thumbnail,scale=640:360 -frames:v 1 -y " + srcPath + "/" + fileName + ".png"
21+
os.system(cmd)
22+
except Exception as e:
23+
print(e)
24+
return

deployment/kubernetes/yaml_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def add_volumeMounts(data, isCDN):
4747
if (isCDN):
4848
volumemounts_caps = [ {'name': 'archive',
4949
'mountPath': '/var/www/archive',
50-
'readOnly': True},
50+
'readOnly': False},
5151
{'name': 'dash',
5252
'mountPath': '/var/www/dash',
5353
'readOnly': False},

script/install_dependency.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ elif [ "$LINUX_DISTRO" == "CentOS" ]; then
7272
try_command yum install -y epel-release
7373
try_command yum install -y python36 python36-pip
7474
try_command pip3 install ruamel.yaml
75+
try_command pip3 install pytest-shutil
7576
else
7677
echo -e $ECHO_PREFIX_INFO "The installation will be cancelled."
7778
echo -e $ECHO_PREFIX_INFO "The CDN-Transcode-Sample does not support this OS, please use Ubuntu 18.04 or CentOS 7.6.\n"

volume/html/css/app.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,30 @@ form .form-icons .fa {
2020
color: white;
2121
width: 1rem;
2222
}
23+
24+
.input-group-0 {
25+
display: none;
26+
}
27+
28+
.flex-center{
29+
width: 100%;
30+
height: 50px;
31+
display: none;
32+
}
33+
34+
.flex-center .input-group-bar {
35+
border-style: solid;
36+
border-width: 1px;
37+
border-color: #cccccc;
38+
width: 80%;
39+
height: 10px;
40+
border-radius: 2px;
41+
margin: 0 auto;
42+
}
43+
.flex-center .bar{
44+
background-color: #ff4c4c;
45+
width: 0%;
46+
height: 100%;
47+
48+
}
49+

volume/html/header.shtml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<li><a href="#" user-name-menu class="menu-text">guest</a>
1212
<ul class="menu" style="background-color:gray">
1313
<li><a href="#" data-open="setting" class="menu-text"><i class="fi-key"></i>&nbsp;Setting</a></li>
14+
<li><a href="#" data-open="upload" class="menu-text"><i class="fi-key"></i>&nbsp;Upload</a></li>
1415
</ul>
1516
</li>
1617
</ul>
@@ -29,3 +30,28 @@
2930
<input class="button expanded" type="submit" data-close>
3031
</form>
3132
</div>
33+
34+
<div class="reveal" id="upload" data-reveal>
35+
<form>
36+
<p><h4><b>Upload</b></h4></p>
37+
38+
<div class="input-group-0" ui-header-setting-user>
39+
<input class="input_file" type="file" name="file" required="required"/>
40+
</div>
41+
42+
<div class="input-group choose-file" ui-header-setting-user>
43+
<span class="input-group-label" style="width:40%"><b>Choose File:</b></span>
44+
<input class="input-group-field" type="text" name="user" readonly required placeholder="Please choose mp4 file"><br>
45+
</div>
46+
47+
48+
<div class="flex-center">
49+
<h6 style="text-align:center">Upload 0% </h6>
50+
<div class="input-group-bar">
51+
<div class="bar"></div>
52+
</div>
53+
</div>
54+
55+
<input class="button expanded" type="button" value="Submit" vadata-close>
56+
</form>
57+
</div>

volume/html/js/app-header.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,86 @@ var settings={
2121
return typeof localStorage.user!="undefined"?localStorage.user:"guest";
2222
},
2323
}
24+
25+
26+
//upload
27+
$("#upload .choose-file").click(
28+
function() {
29+
$("#upload .input_file").trigger('click')
30+
}
31+
)
32+
33+
$("#upload .input_file").change(
34+
function() {
35+
var filePath = $("#upload .input_file").val()
36+
var pos=filePath.lastIndexOf("\\");
37+
var fileName = filePath.substring(pos+1);
38+
$("#upload .choose-file .input-group-field").val(fileName)
39+
}
40+
)
41+
42+
$("#upload").find("form .button").click(function() {
43+
if(!$("#upload .input_file").val()){
44+
return false
45+
}
46+
if (!(document.querySelector("#upload .input_file").files[0].name.endsWith('.mp4'))) {
47+
$(".flex-center").show()
48+
$(".flex-center h6").html("Please choose mp4 file")
49+
$(".flex-center .input-group-bar").hide()
50+
return false
51+
} else {
52+
upload()
53+
}
54+
});
55+
56+
function upload() {
57+
$(".flex-center").show()
58+
$(".flex-center .input-group-bar").show()
59+
const LENGTH = 1024 * 1024 * 10;
60+
var timeStamp = new Date().getTime();
61+
var fileName=$("#upload .choose-file .input-group-field").val();
62+
var file = document.querySelector('#upload .input_file').files[0];
63+
var totalSize = file.size;
64+
var start = 0;
65+
var end = start + LENGTH;
66+
var fd = null;
67+
var blob = null;
68+
var xhr = null;
69+
var sum = Math.ceil(totalSize/LENGTH)
70+
var count = 0
71+
var timer = setInterval(function () {
72+
if (start < totalSize) {
73+
fd = new FormData();
74+
xhr = new XMLHttpRequest();
75+
xhr.open('POST', '/upload/', false);
76+
blob = file.slice(start, end);
77+
fd.append('file', blob);
78+
fd.append('fileName', fileName);
79+
fd.append('timeStamp', timeStamp);
80+
if (end >= totalSize) {
81+
fd.append('uploadStatus','end')
82+
}
83+
xhr.send(fd);
84+
if (xhr.status != 200 && xhr.status != 0) {
85+
$(".flex-center h6").html("Error, Please try again")
86+
$(".flex-center .input-group-bar").hide()
87+
return false
88+
}
89+
count += 1
90+
$(".flex-center h6").html("Upload " + parseInt(count * 100 / sum) + "%")
91+
$(".flex-center .bar").width(parseInt(count * 100 / sum) + "%")
92+
start = end;
93+
end = start + LENGTH;
94+
} else {
95+
$(".flex-center h6").html("Upload 0%")
96+
$(".flex-center .bar").width("0%")
97+
$(".flex-center").hide()
98+
$("#upload .input_file").val('')
99+
$("#upload .choose-file .input-group-field").val('')
100+
$("#setting").find("form").submit();
101+
$(".reveal-overlay").trigger('click');
102+
clearInterval(timer);
103+
}
104+
},100)
105+
}
106+

0 commit comments

Comments
 (0)