Commit bd2ddb2a authored by Jeff nan's avatar Jeff nan

feat

parent d8a8d701
<div class="p-video-box">
<div class="up-video" style="display: flex;">
<nz-upload class="p-image-uploader" [nzDisabled]="uploading" [nzShowUploadList]="false" nzAccept=".mp4"
(nzChange)="handleChange($event)" [nzCustomRequest]="customReq"
[nzAction]="uploadUrl" [nzData]="uploadData"
(nzChange)="handleChange($event)"
style="margin-right: 1rem">
<button type="button" nz-button nzType="default" *ngIf="showUploadBtn" [disabled]="uploading"
[nzLoading]="uploading">
......@@ -40,9 +41,9 @@
<i nz-icon nzType="loading" nzTheme="outline"></i>Checking...
</div>
</div>
<div class="p-preview" *ngIf="!uploading&&playVedioUrl">
<video crossorigin="anonymous" [src]="playVedioUrl" controls #videoNode></video>
<!-- <video crossorigin="anonymous" [src]="safeVideoUrl()" controls #videoNode></video> -->
<div class="p-preview" *ngIf="!uploading && videoUrl ">
<video crossorigin="anonymous" [src]="safeVideoUrl(videoUrl)" controls #videoNode></video>
<!-- <video crossorigin="anonymous" [src]="videoUrl" controls #videoNode></video> -->
</div>
</div>
<div [style.display]="!checkVideoExists?'none':''">
......
......@@ -5,38 +5,7 @@ import {HttpClient, HttpHeaders, HttpRequest, HttpEvent, HttpEventType, HttpResp
import {NzMessageService, UploadChangeParam, UploadFile, UploadFileStatus, UploadXHRArgs} from 'ng-zorro-antd';
import {DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';
import cryptoJs from 'crypto-js';
import {v4} from 'uuid';
function httpHeadCall(requsetUrl, callback) {
let xhr = new XMLHttpRequest();
try {
xhr.onreadystatechange = () => {
try {
if (xhr.readyState == 4) {
if ((xhr.status >= 200 && xhr.status < 400)) {
callback(true);
} else {
callback(false);
}
}
} catch (e) {
callback(false);
}
};
xhr.open("HEAD", requsetUrl, true);
xhr.send();
xhr.timeout = 15000;
xhr.onerror = (e) => {
callback(false);
};
xhr.ontimeout = (e) => {
callback(false);
};
} catch (e) {
callback(false);
}
}
@Component({
......@@ -102,24 +71,13 @@ export class UploadVideoComponent implements OnChanges, OnDestroy, OnInit {
this.uploadUrl = (<any> window).courseware.uploadVideoUrl();
this.uploadData = (<any> window).courseware.uploadData();
window['air'].getUploadVideoCallback = (url, data) => {
this.uploadUrl = url;
this.uploadData = data;
};
}
ngOnInit() {
this.playVedioUrl = this.videoUrl.replace(`_h.mp4`, `_l.mp4`);;
}
md5Short(str, _salt) {
return cryptoJs.MD5(str + _salt).toString();
}
ngOnChanges() {
// if (!this.videoFile || this.showUploadBtn) {
......@@ -132,94 +90,13 @@ export class UploadVideoComponent implements OnChanges, OnDestroy, OnInit {
URL.revokeObjectURL(this.videoUrl);
}
playVedioUrl = '';
safeVideoUrl() {
const url = this.videoUrl.replace(`_h.mp4`, `.mp4`).replace(`.mp4`, `_l.mp4`);
console.log(`=============safeVideoUrl==============`,url);
httpHeadCall(url, (flag)=>{
if (flag) {
this.playVedioUrl = url;
} else {
setTimeout(() => {
this.safeVideoUrl();
}, 500);
}
});
return this.playVedioUrl;
safeVideoUrl(url) {
return this.sanitization.bypassSecurityTrustResourceUrl(url); // `${url}`;
}
videoLoadedMetaData() {
}
// fileToBlob(file) {
// let reader = new FileReader();
// reader.addEventListener('load', (e) => {
// let base64 = e.target.result;
// let blob = new Blob([base64], { type: file.type });
// console.log("Blob对象", blob);
// })
// reader.readAsDataURL(file)
// }
customReq(item: UploadXHRArgs) {
// md5值计算
const protocolStr = document.location.protocol;
const baseCDN = `${protocolStr}//teach.cdn.ireadabc.com`;
const baseAPI = `${protocolStr}//openapi.iteachabc.com`;
const fileName = v4().replace(/-/g, "");
httpHeadCall(`${baseCDN}/${fileName}_h.mp4`, (flag)=>{
if (flag) {
item.onSuccess!({url: `${baseCDN}/${fileName}.mp4`}, item.file!, event);
} else {
this.http.get(`${baseAPI}/oss/timestamp`, {}).subscribe((tmps: string) => {
const sign = cryptoJs.MD5(tmps+"iplayABC2019").toString();
const params = new HttpParams().set('sign', sign).set('stmp', tmps);
this.http.get(`${baseAPI}/oss/signature`, {params}).subscribe((res) => {
const serve = res['data'];
const formData = new FormData();
formData.append('key', fileName+`.mp4`);
formData.append('bucket', `iplayabc-worker-queue-videolmh`);
formData.append('OSSAccessKeyId', serve.OSSAccessKeyId);
formData.append('policy', serve.policy);
formData.append('Signature', serve.signature);
formData.append('success_action_status', '200');
formData.append('file', item.file as any);
//设置cors跨域请求
const headerstemp = new HttpHeaders({ 'Access-Control-Allow-Origin': '*' });
item.action = `${protocolStr}//iplayabc-worker-queue-videolmh.oss-cn-beijing.aliyuncs.com`;
const req = new HttpRequest('POST', item.action!, formData, {
headers: headerstemp,
reportProgress: true, //进度条
// withCredentials: true
});
// Always returns a `Subscription` object. nz-upload would automatically unsubscribe it at correct time.
return this.http.request(req).subscribe(
(event: HttpEvent<{}>) => {
if (event.type === HttpEventType.UploadProgress) {
if (event.total! > 0) {
// tslint:disable-next-line:no-any
(event as any).percent = (event.loaded / event.total!) * 100;
}
item.onProgress!(event, item.file!);
} else if (event instanceof HttpResponse) {
item.onSuccess!({url: `${baseCDN}/${fileName}.mp4`}, item.file!, event);
}
},
err => {
console.log(err);
item.onError!(err, item.file!);
}
);
});
});
}
});
};
handleChange(info: UploadChangeParam/* { type: string, file: UploadFile, event: any }*/): void {
console.log(info);
......@@ -235,8 +112,7 @@ export class UploadVideoComponent implements OnChanges, OnDestroy, OnInit {
this.uploadSuccess(info.file);
break;
case 'progress':
this.progress = info.event.percent;
// this.doProgress(this.progress);
this.doProgress(info.event.percent);
break;
}
}
......@@ -277,10 +153,38 @@ export class UploadVideoComponent implements OnChanges, OnDestroy, OnInit {
this.videoFile = null;
// this.updateSource(url);
this.videoUrl = file.response.url;
this.videoUploaded.emit(file.response);
setTimeout(() => {
this.safeVideoUrl();
}, 2000);
// console.log(this.picUrl)
// this.uploadFinished(url);
// if (!inOSS) {
const vid = document.createElement('video');
vid.addEventListener('loadedmetadata', () => {
const height = vid.videoHeight;
const width = vid.videoWidth;
let duration = vid.duration;
if (duration) {
duration = duration * 1000;
}
file.height = height;
file.width = width;
file.duration = duration;
vid.preload = 'none';
vid.src = '';
vid.remove();
this.videoUploaded.emit(file.response);
// this.resService.updateVideo(id, {width, height, duration}).then( () => {
// this.videoUploaded.emit({res_id: id, id, name, hash, url});
// });
});
vid.src = file.response.url;
// } else {
// this.videoUploaded.emit(file.response);
// }
}
......@@ -297,7 +201,8 @@ export class UploadVideoComponent implements OnChanges, OnDestroy, OnInit {
doProgress = (p) => {
if (p > 1) {
p = 1;
this.progress = p;
return;
}
if (p < 0) {
p = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment