Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { FileInfo } from './../models/FileInfo'
  2. import { VenuesService } from './venues.service'
  3. import { ApiRequest } from './api.service'
  4. import { Injectable } from '@angular/core'
  5. import { FilePath } from '@ionic-native/file-path'
  6. import { File } from '@ionic-native/file'
  7. import { Transfer, TransferObject } from '@ionic-native/transfer'
  8. import { Camera } from '@ionic-native/camera'
  9. import { Platform } from 'ionic-angular'
  10.  
  11. declare var cordova
  12.  
  13. @Injectable()
  14. export class PhotosService {
  15.  
  16.     private lastImage: string = null
  17.  
  18.     constructor(public camera: Camera, public file: File, public platform: Platform, public filePath: FilePath, public api: ApiRequest, public transfer: Transfer, public venuesService: VenuesService) { }
  19.  
  20.     // Copy the image to a local folder
  21.     copyFileToLocalDir(finfo: FileInfo): Promise<string> {
  22.         return this.file.copyFile(finfo.path, finfo.currentName, cordova.file.dataDirectory, finfo.destName)
  23.             .then(success => {
  24.                 this.lastImage = finfo.destName
  25.                 return Promise.resolve(this.lastImage)
  26.             })
  27.             .catch(error => Promise.reject('Erreur lors de l\'écriture de l\'image sur le disque.'))
  28.     }
  29.  
  30.     public takePicture(sourceType): Promise<string> {
  31.         // Create options for the Camera Dialog
  32.         let options = {
  33.             quality: 100,
  34.             sourceType: sourceType,
  35.             saveToPhotoAlbum: false,
  36.             correctOrientation: true
  37.         }
  38.  
  39.         // Get the data of an image
  40.         return this.camera.getPicture(options)
  41.             .then((imagePath) => this.getfileInfos(sourceType, imagePath))
  42.             .then((finfo: FileInfo) => this.copyFileToLocalDir(finfo))
  43.     }
  44.  
  45.     public upload(venueId: number, fileName: string) {
  46.         // Destination URL
  47.         const url = this.api.baseUrl + "/salle/" + venueId + '/upload'
  48.  
  49.         // File for Upload
  50.         const targetPath = this.pathForImage(this.lastImage)
  51.  
  52.         const headers = {
  53.             Connection: 'close',
  54.             Authorization: this.api.oauthData.token_type + ' ' + this.api.oauthData.access_token
  55.         }
  56.  
  57.         // options for File Transfer
  58.         const options = {
  59.             fileKey: "file",
  60.             fileName: fileName,
  61.             chunkedMode: false,
  62.             params: {
  63.                 fileName: fileName,
  64.                 idSalle: venueId,
  65.                 type: 1,
  66.                 object: null,
  67.                 headers: headers
  68.             },
  69.             mimeType: 'multipart/form-data',
  70.             headers: headers
  71.         }
  72.  
  73.         const fileTransfer: TransferObject = this.transfer.create()
  74.  
  75.         // Use the FileTransfer to upload the image
  76.         return this.venuesService.getVenueFullDetail(venueId)
  77.             .then(() => fileTransfer.upload(targetPath, url, options).catch(err => Promise.reject('Erreur pendant l\'upload des fichiers.')))
  78.     }
  79.  
  80.     // Get the accurate path to app's folder
  81.     public pathForImage(img) {
  82.         if (img === null) {
  83.             return ''
  84.         } else {
  85.             return cordova.file.dataDirectory + img
  86.         }
  87.     }
  88.  
  89.     private getfileInfos(sourceType, imagePath): Promise<FileInfo> {
  90.         return new Promise((resolve, reject) => {
  91.             let finfo: FileInfo = new FileInfo()
  92.  
  93.             // special handling for Android library
  94.             if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
  95.                 this.filePath.resolveNativePath(imagePath)
  96.                     .then(filePath => {
  97.                         finfo.path = filePath.substr(0, filePath.lastIndexOf('/') + 1)
  98.                         finfo.currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'))
  99.                         finfo.destName = this.createFileName()
  100.                         resolve(finfo)
  101.                     })
  102.                     .catch(() => reject('Erreur lors de la récupération de l\'image'))
  103.             } else {
  104.                 finfo.path = imagePath.substr(0, imagePath.lastIndexOf('/') + 1)
  105.                 finfo.currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1)
  106.                 finfo.destName = this.createFileName()
  107.                 return resolve(finfo)
  108.             }
  109.         })
  110.     }
  111.  
  112.     // Create a new name for the image
  113.     private createFileName(): string {
  114.         let d = new Date()
  115.         return d.getTime() + ".jpg"
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement