Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { FileInfo } from './../models/FileInfo'
- import { VenuesService } from './venues.service'
- import { ApiRequest } from './api.service'
- import { Injectable } from '@angular/core'
- import { FilePath } from '@ionic-native/file-path'
- import { File } from '@ionic-native/file'
- import { Transfer, TransferObject } from '@ionic-native/transfer'
- import { Camera } from '@ionic-native/camera'
- import { Platform } from 'ionic-angular'
- declare var cordova
- @Injectable()
- export class PhotosService {
- private lastImage: string = null
- constructor(public camera: Camera, public file: File, public platform: Platform, public filePath: FilePath, public api: ApiRequest, public transfer: Transfer, public venuesService: VenuesService) { }
- // Copy the image to a local folder
- copyFileToLocalDir(finfo: FileInfo): Promise<string> {
- return this.file.copyFile(finfo.path, finfo.currentName, cordova.file.dataDirectory, finfo.destName)
- .then(success => {
- this.lastImage = finfo.destName
- return Promise.resolve(this.lastImage)
- })
- .catch(error => Promise.reject('Erreur lors de l\'écriture de l\'image sur le disque.'))
- }
- public takePicture(sourceType): Promise<string> {
- // Create options for the Camera Dialog
- let options = {
- quality: 100,
- sourceType: sourceType,
- saveToPhotoAlbum: false,
- correctOrientation: true
- }
- // Get the data of an image
- return this.camera.getPicture(options)
- .then((imagePath) => this.getfileInfos(sourceType, imagePath))
- .then((finfo: FileInfo) => this.copyFileToLocalDir(finfo))
- }
- public upload(venueId: number, fileName: string) {
- // Destination URL
- const url = this.api.baseUrl + "/salle/" + venueId + '/upload'
- // File for Upload
- const targetPath = this.pathForImage(this.lastImage)
- const headers = {
- Connection: 'close',
- Authorization: this.api.oauthData.token_type + ' ' + this.api.oauthData.access_token
- }
- // options for File Transfer
- const options = {
- fileKey: "file",
- fileName: fileName,
- chunkedMode: false,
- params: {
- fileName: fileName,
- idSalle: venueId,
- type: 1,
- object: null,
- headers: headers
- },
- mimeType: 'multipart/form-data',
- headers: headers
- }
- const fileTransfer: TransferObject = this.transfer.create()
- // Use the FileTransfer to upload the image
- return this.venuesService.getVenueFullDetail(venueId)
- .then(() => fileTransfer.upload(targetPath, url, options).catch(err => Promise.reject('Erreur pendant l\'upload des fichiers.')))
- }
- // Get the accurate path to app's folder
- public pathForImage(img) {
- if (img === null) {
- return ''
- } else {
- return cordova.file.dataDirectory + img
- }
- }
- private getfileInfos(sourceType, imagePath): Promise<FileInfo> {
- return new Promise((resolve, reject) => {
- let finfo: FileInfo = new FileInfo()
- // special handling for Android library
- if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
- this.filePath.resolveNativePath(imagePath)
- .then(filePath => {
- finfo.path = filePath.substr(0, filePath.lastIndexOf('/') + 1)
- finfo.currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'))
- finfo.destName = this.createFileName()
- resolve(finfo)
- })
- .catch(() => reject('Erreur lors de la récupération de l\'image'))
- } else {
- finfo.path = imagePath.substr(0, imagePath.lastIndexOf('/') + 1)
- finfo.currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1)
- finfo.destName = this.createFileName()
- return resolve(finfo)
- }
- })
- }
- // Create a new name for the image
- private createFileName(): string {
- let d = new Date()
- return d.getTime() + ".jpg"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement