src/app/manage-learn/shared/components/image-upload/image-upload.component.ts
OnInit
selector | app-image-upload |
styleUrls | ./image-upload.component.scss |
templateUrl | ./image-upload.component.html |
Properties |
Methods |
Inputs |
Accessors |
constructor(actionSheet: ActionSheetController, camera: Camera, localStorage: LocalStorageService, file: File, imgPicker: ImagePicker, utils: UtilsService, photoLibrary: PhotoLibrary, platform: Platform, translate: TranslateService, filePath: FilePath, fileOpener: FileOpener, fileChooser: FileChooser, chooser: Chooser, androidPermissions: AndroidPermissions, diagnostic: Diagnostic, media: Media, alertCtrl: AlertController, toast: ToastService, popupService: GenericPopUpService, attachmentService: AttachmentService)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Parameters :
|
data | |
Type : any
|
|
evidenceId | |
Type : any
|
|
generalQuestion | |
Type : boolean
|
|
imageLocalCopyId | |
Type : string
|
|
schoolId | |
Type : string
|
|
submissionId | |
Type : any
|
|
checkForLocalFolder | ||||
checkForLocalFolder(imagePath)
|
||||
Parameters :
Returns :
void
|
checkRecordMediaPermission |
checkRecordMediaPermission()
|
Returns :
void
|
copyFileToLocalDir | ||||||
copyFileToLocalDir(namePath, currentName)
|
||||||
Parameters :
Returns :
void
|
createFileName | ||||
createFileName(filename)
|
||||
Parameters :
Returns :
string
|
createImageFromName | ||||
createImageFromName(imageList)
|
||||
Parameters :
Returns :
void
|
Async deleteImageAlert | ||||
deleteImageAlert(index)
|
||||
Parameters :
Returns :
any
|
doAction |
doAction()
|
Returns :
void
|
getExtensionFromName | ||||
getExtensionFromName(fileName)
|
||||
Parameters :
Returns :
any
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
Async openActionSheet |
openActionSheet()
|
Returns :
any
|
previewFile | ||||||
previewFile(fileName, extension)
|
||||||
Parameters :
Returns :
void
|
pushToFileList | ||||
pushToFileList(fileName)
|
||||
Parameters :
Returns :
void
|
removeImgFromList | ||||
removeImgFromList(index)
|
||||
Parameters :
Returns :
void
|
setLocalDatas | ||||
setLocalDatas(fileName)
|
||||
Parameters :
Returns :
void
|
startRecord |
startRecord()
|
Returns :
void
|
startTimer |
startTimer()
|
Returns :
void
|
stopRecord |
stopRecord()
|
Returns :
void
|
updateLocalImageList |
updateLocalImageList()
|
Returns :
void
|
appFolderPath |
Type : string
|
audio |
Type : MediaObject
|
audioFormats |
Type : []
|
Default value : ["AIF", "cda", "mpa", "ogg", "wav", "wma", "mp3"]
|
audioList |
Type : any[]
|
Default value : []
|
datas |
fileName |
Type : string
|
filesPath |
Type : string
|
imageFormats |
Type : []
|
Default value : ["jpg", "png", "jpeg"]
|
imageList |
Type : Array<any>
|
Default value : []
|
imageNameCounter |
Type : number
|
Default value : 0
|
interval |
isIos |
Type : boolean
|
Default value : this.platform.is("ios")
|
localEvidenceImageList |
Type : any
|
mediaObject |
minutes |
Type : number
|
Default value : 0
|
pdfFormats |
Type : []
|
Default value : ["pdf"]
|
pptFormats |
Type : []
|
Default value : ["ppt", "pptx", "pps", "ppsx"]
|
recording |
Type : boolean
|
Default value : false
|
seconds |
Type : number
|
Default value : 0
|
spreadSheetFormats |
Type : []
|
Default value : ["xls", "xlsx"]
|
text |
Type : string
|
timeLeft |
Type : number
|
Default value : 0
|
videoFormats |
Type : []
|
Default value : ["mp4", "WMV", "WEBM", "flv", "avi", "3GP", "OGG","mov"]
|
wordFormats |
Type : []
|
Default value : ["docx", "doc", "docm", "dotx"]
|
data | ||||
setdata(data)
|
||||
Parameters :
Returns :
void
|
name |
getname()
|
import { Component, OnInit, Input } from '@angular/core';
import { Camera, CameraOptions } from '@ionic-native/camera/ngx';
import { File } from '@ionic-native/file/ngx';
import { ImagePicker, ImagePickerOptions } from '@ionic-native/image-picker/ngx';
import { TranslateService } from "@ngx-translate/core";
import { FileChooser } from '@ionic-native/file-chooser/ngx';
import { FilePath } from '@ionic-native/file-path/ngx';
import { PhotoLibrary } from '@ionic-native/photo-library/ngx';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { Media, MediaObject } from '@ionic-native/media/ngx';
import { AndroidPermissions } from '@ionic-native/android-permissions/ngx';
import { Diagnostic } from '@ionic-native/diagnostic/ngx';
import { AttachmentService, FILE_EXTENSION_HEADERS, LocalStorageService, ToastService, UtilsService } from '@app/app/manage-learn/core';
import { ActionSheetController, AlertController, Platform } from '@ionic/angular';
import { GenericPopUpService } from '../../generic.popup';
import { Chooser } from '@ionic-native/chooser/ngx';
@Component({
selector: 'app-image-upload',
templateUrl: './image-upload.component.html',
styleUrls: ['./image-upload.component.scss'],
})
export class ImageUploadComponent implements OnInit {
recording: boolean = false;
filesPath: string;
fileName: string;
audio: MediaObject;
audioList: any[] = [];
isIos: boolean = this.platform.is("ios");
interval;
timeLeft: number = 0;
minutes: number = 0;
seconds: number = 0;
text: string;
datas;
appFolderPath: string;
videoFormats = ["mp4", "WMV", "WEBM", "flv", "avi", "3GP", "OGG","mov"];
audioFormats = ["AIF", "cda", "mpa", "ogg", "wav", "wma", "mp3"];
pptFormats = ["ppt", "pptx", "pps", "ppsx"];
wordFormats = ["docx", "doc", "docm", "dotx"];
imageFormats = ["jpg", "png", "jpeg"];
pdfFormats = ["pdf"];
spreadSheetFormats = ["xls", "xlsx"];
@Input()
set data(data) {
this.datas = data;
this.createImageFromName(data["fileName"]);
}
get name() {
return true;
}
@Input() evidenceId: any;
@Input() schoolId: string;
@Input() submissionId: any;
@Input() imageLocalCopyId: string;
@Input() generalQuestion: boolean;
imageList: Array<any> = [];
imageNameCounter: number = 0;
localEvidenceImageList: any;
constructor(
private actionSheet: ActionSheetController,
private camera: Camera,
private localStorage: LocalStorageService,
private file: File,
private imgPicker: ImagePicker,
private utils: UtilsService,
private photoLibrary: PhotoLibrary,
private platform: Platform,
private translate: TranslateService,
private filePath: FilePath,
private fileOpener: FileOpener,
private fileChooser: FileChooser,
private chooser: Chooser,
private androidPermissions: AndroidPermissions,
private diagnostic: Diagnostic,
private media: Media,
private alertCtrl: AlertController,
private toast: ToastService,
private popupService: GenericPopUpService,
private attachmentService :AttachmentService
) {
this.text = "Hello World";
this.isIos = this.platform.is("ios") ? true : false;
if (this.isIos) {
this.file
.checkDir(this.file.documentsDirectory, "images")
.then((success) => { })
.catch((err) => {
this.file
.createDir(cordova.file.documentsDirectory, "images", false)
.then(
(success) => { },
(error) => { }
);
});
} else {
this.file
.checkDir(this.file.externalDataDirectory, "images")
.then((success) => { })
.catch((err) => {
this.file
.createDir(cordova.file.externalDataDirectory, "images", false)
.then(
(success) => { },
(error) => { }
);
});
}
}
ngOnInit() {
this.appFolderPath = this.isIos
? cordova.file.documentsDirectory + "images"
: cordova.file.externalDataDirectory + "images";
}
doAction() {
this.popupService.showPPPForProjectPopUp('FRMELEMNTS_LBL_EVIDENCES_CONTENT_POLICY', 'FRMELEMNTS_LBL_EVIDENCES_CONTENT_POLICY_TEXT', 'FRMELEMNTS_LBL_EVIDENCES_CONTENT_POLICY_LABEL', 'FRMELEMNTS_LBL_UPLOAD_EVIDENCES', 'https://diksha.gov.in/term-of-use.html', 'contentPolicy').then((data: any) => {
if (data.isClicked) {
data.isChecked ? this.openActionSheet() : this.toast.showMessage('FRMELEMNTS_MSG_EVIDENCES_CONTENT_POLICY_REJECT', 'danger');
}
})
}
async openActionSheet() {
this.attachmentService.evidenceUpload(this.appFolderPath).then(data =>{
if(data.data){
if(data.data.multiple &&data.data.imageData ){
for (const image of data.data.imageData) {
this.checkForLocalFolder(image);
}
}else {
this.pushToFileList(data.data.name);
}
}
})
}
checkForLocalFolder(imagePath) {
let currentName = imagePath.substr(imagePath.lastIndexOf("/") + 1);
let currentPath = imagePath.substr(0, imagePath.lastIndexOf("/") + 1);
if (this.isIos) {
this.file
.checkDir(this.file.documentsDirectory, "images")
.then((success) => {
this.copyFileToLocalDir(currentPath, currentName);
})
.catch((err) => {
this.file
.createDir(cordova.file.documentsDirectory, "images", false)
.then(
(success) => {
this.copyFileToLocalDir(currentPath, currentName);
},
(error) => { }
);
});
} else {
this.file
.checkDir(this.file.externalDataDirectory, "images")
.then((success) => {
this.copyFileToLocalDir(currentPath, currentName);
})
.catch((err) => {
this.file
.createDir(cordova.file.externalDataDirectory, "images", false)
.then(
(success) => {
this.copyFileToLocalDir(currentPath, currentName);
},
(error) => { }
);
});
}
}
createFileName(filename) {
let d = new Date(),
n = d.getTime(),
extension= filename.split('.').pop(),
newFileName = n + "." + extension;
return newFileName;
}
copyFileToLocalDir(namePath, currentName) {
let newName = this.createFileName(currentName);
this.file
.copyFile(namePath, currentName, this.appFolderPath, newName)
.then(
(success) => {
this.pushToFileList(newName);
},
(error) => { }
);
}
pushToFileList(fileName) {
this.file
.checkFile(this.appFolderPath + "/", fileName)
.then((response) => {
this.imageList.push({
data: "",
imageName: fileName,
extension: this.getExtensionFromName(fileName),
});
this.setLocalDatas(fileName);
})
}
setLocalDatas(fileName) {
this.datas.fileName.push(fileName);
this.updateLocalImageList();
}
getExtensionFromName(fileName) {
let splitString = fileName.split(".");
let extension = splitString[splitString.length - 1];
return extension;
}
createImageFromName(imageList) {
this.isIos = this.platform.is("ios") ? true : false;
this.appFolderPath = this.isIos
? cordova.file.documentsDirectory + "images"
: cordova.file.externalDataDirectory + "images";
for (const image of imageList) {
this.file
.checkFile(this.appFolderPath + "/", image)
.then((response) => {
this.imageList.push({
data: "",
imageName: image,
extension: this.getExtensionFromName(image),
});
})
.catch((error) => {
this.imageList.push(image);
});
}
}
removeImgFromList(index): void {
let indexInLocalList;
this.datas.fileName.splice(index, 1);
this.imageList.splice(index, 1);
this.updateLocalImageList();
}
async deleteImageAlert(index) {
let translateObject;
this.translate
.get([
"FRMELEMNTS_LBL_COFIRM_DELETE",
"actionSheet.confirmDeleteInstance",
"FRMELEMNTS_LBL_NO",
"FRMELEMNTS_LBL_YES",
])
.subscribe((translations) => {
translateObject = translations;
});
let alert = await this.alertCtrl.create({
// header: translateObject["FRMELEMNTS_LBL_COFIRM_DELETE"],
message: translateObject["FRMELEMNTS_LBL_COFIRM_DELETE"],
cssClass:'central-alert',
buttons: [
{
text: translateObject["FRMELEMNTS_LBL_NO"],
role: "cancel",
handler: () => { },
},
{
text: translateObject["FRMELEMNTS_LBL_YES"],
handler: () => {
this.removeImgFromList(index);
},
},
],
});
await alert.present();
}
updateLocalImageList() {
}
previewFile(fileName, extension) {
this.fileOpener
.open(
this.appFolderPath + "/" + fileName,
FILE_EXTENSION_HEADERS[extension]
)
.then(() => console.log("File is opened"))
.catch((e) => {
this.toast.openToast("No file readers available")
});
}
mediaObject;
startRecord() {
if (this.platform.is("ios")) {
this.file
.checkDir(this.file.documentsDirectory, "images")
.then((success) => {
this.fileName =
"record" +
new Date().getDate() +
new Date().getMonth() +
new Date().getFullYear() +
new Date().getHours() +
new Date().getMinutes() +
new Date().getSeconds() +
".mp3";
this.filesPath =
this.file.documentsDirectory + "images/" + this.fileName;
this.file
.createFile(this.file.tempDirectory, this.fileName, true)
.then(() => {
this.mediaObject = this.media.create(
this.file.tempDirectory.replace(/^file:\/\//, "") +
this.fileName
);
this.mediaObject.startRecord();
this.startTimer();
})
.catch((error) => {
this.toast.openToast("Something went wrong");
});
})
.catch((err) => {
this.file
.createDir(cordova.file.documentsDirectory, "images", false)
.then(
(success) => {
this.fileName =
"record" +
new Date().getDate() +
new Date().getMonth() +
new Date().getFullYear() +
new Date().getHours() +
new Date().getMinutes() +
new Date().getSeconds() +
".mp3";
this.filesPath =
this.file.documentsDirectory + "images/" + this.fileName;
this.mediaObject = this.media.create(
this.file.tempDirectory.replace(/^file:\/\//, "") +
this.fileName
);
this.mediaObject.startRecord();
this.startTimer();
},
(error) => { }
);
});
} else if (this.platform.is("android")) {
this.file
.checkDir(this.file.externalDataDirectory, "images")
.then((success) => {
this.fileName =
"record" +
new Date().getDate() +
new Date().getMonth() +
new Date().getFullYear() +
new Date().getHours() +
new Date().getMinutes() +
new Date().getSeconds() +
".mp3";
this.filesPath =
this.file.externalDataDirectory + "images/" + this.fileName;
this.audio = this.media.create(this.filesPath);
this.startTimer();
this.audio.startRecord();
})
.catch((err) => {
this.file
.createDir(cordova.file.externalDataDirectory, "images", false)
.then(
(success) => {
this.fileName =
"record" +
new Date().getDate() +
new Date().getMonth() +
new Date().getFullYear() +
new Date().getHours() +
new Date().getMinutes() +
new Date().getSeconds() +
".mp3";
this.filesPath =
this.file.externalDataDirectory + "images/" + this.fileName;
this.audio = this.media.create(this.filesPath);
this.startTimer();
this.audio.startRecord();
},
(error) => { }
);
});
}
}
startTimer() {
this.recording = true;
if (this.recording) {
this.interval = setInterval(() => {
if (this.timeLeft >= 0) {
this.timeLeft++;
this.minutes = Math.ceil(this.timeLeft / 60) - 1;
this.seconds = Math.floor(this.timeLeft % 60);
} else {
this.timeLeft = 0;
this.minutes = 0;
this.seconds = 0;
}
}, 1000);
}
}
checkRecordMediaPermission() {
this.diagnostic
.isMicrophoneAuthorized()
.then((success) => {
this.diagnostic
.requestMicrophoneAuthorization()
.then((success) => {
if (success === "authorized" || success === "GRANTED") {
const permissionsArray = [
this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,
this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE,
this.androidPermissions.PERMISSION.RECORD_AUDIO,
];
this.androidPermissions
.requestPermissions(permissionsArray)
.then((successResult) => {
successResult.hasPermission
? this.startRecord()
: this.toast.openToast(
"Please accept the permissions to use this feature"
);
})
.catch((error) => {
this.toast.openToast(
"Please accept the permissions to use this feature"
);
});
} else {
this.toast.openToast(
"Please accept the permissions to use this feature"
);
}
})
.catch((error) => {
console.log("Please accept the permissions to use this feature");
});
})
}
stopRecord() {
this.recording = false;
this.timeLeft = 0;
this.minutes = 0;
this.seconds = 0;
clearInterval(this.interval);
if (this.isIos) {
this.mediaObject.stopRecord();
this.mediaObject.release();
this.file
.copyFile(
this.file.tempDirectory,
this.fileName,
this.appFolderPath,
this.fileName
)
.then(
(success) => {
this.file.removeFile(this.file.tempDirectory, this.fileName);
this.pushToFileList(this.fileName);
},
(error) => { }
);
} else {
this.audio.stopRecord();
this.audio.release();
this.pushToFileList(this.fileName);
}
}
}
<div class="_flex-box ion-margin-top" style="flex-wrap: wrap;">
<div *ngIf="datas?.file" class="_cards _flex-box _justify-content-center _flex-direction-column ion-padding" >
<div class="textAdj" (click)="doAction()">
<div class=" _flex-box _justify-content-center _flex-direction-column">
<ion-icon name="add" class="_iconAdj"></ion-icon>
</div>
<div>{{'FRMELEMENTS_LBL_FILES'|translate}}</div>
</div>
<div class="_overlay" (click)="doAction()"></div>
</div>
<div class="_cards _flex-box _justify-content-center" *ngFor="let image of imageList; let i = index">
<ion-icon name="trash" class="_closeBtn" (click)="deleteImageAlert(i)"></ion-icon>
<div class="_overlay" (click)="previewFile(image?.imageName,image?.extension)"></div>
<img [src]="image?.data" alt="">
<span>
<ion-icon name="document" *ngIf="pdfFormats.includes(image?.extension) || wordFormats.includes(image?.extension) || pptFormats.includes(image?.extension) "></ion-icon>
<ion-icon name="musical-notes" *ngIf="audioFormats.includes(image?.extension)"></ion-icon>
<ion-icon name="videocam" *ngIf="videoFormats.includes(image?.extension)"></ion-icon>
<ion-icon class="attachmentIcon" name="image" *ngIf="imageFormats.includes(image?.extension)"></ion-icon>
</span>
</div>
<ion-card *ngIf="datas?.allowAudioRecording">
<ion-card-content>
<ion-row *ngIf="!recording">
<button ion-button primary (click)="checkRecordMediaPermission()">
<ion-icon name="mic"></ion-icon> Start Record
</button>
</ion-row>
<ion-row *ngIf="recording" class="_flex-box _justify-content-space-between">
<button ion-button primary (click)="stopRecord()">
<ion-icon name="mic-off"></ion-icon> Stop Record
</button>
<h1>
<span *ngIf="minutes < 10">0</span>{{minutes}} : <span *ngIf="seconds < 10">0</span>{{seconds}}
</h1>
</ion-row>
</ion-card-content>
</ion-card>
</div>
./image-upload.component.scss
.attachmentIcon {
margin-top: 10px;
font-size: 3.125rem;
}
.textAdj {
z-index: 3;
}
._closeBtn {
position: absolute;
top: 0.313rem;
right: 0.625rem;
color: red;
z-index: 3;
font-weight: 600;
font-size: 1.563rem;
cursor: pointer;
}