From 50b58cc16f6f4ed01a7664ad1b3483aaa8c655b0 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Mon, 19 Feb 2018 07:04:16 -0800 Subject: [PATCH] file upload to pnut --- src/app/app.component.ts | 3 ++- src/app/app.module.ts | 9 +++++++ src/pages/login/login.ts | 2 +- src/pages/stream/stream.ts | 49 ++++++++++++++++++++++++++++++++++++-- 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index c56007a..7791ef2 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -3,6 +3,7 @@ import { Nav, Platform } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen'; import { Storage } from '@ionic/storage'; +import { Device } from '@ionic-native/device'; import { LoginPage } from '../pages/login/login'; import { StreamPage } from '../pages/stream/stream'; @@ -21,7 +22,7 @@ export class MyApp { pages: Array<{title: string, component: any, params: Object}>; constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, - private storage: Storage) { + private storage: Storage, private device: Device) { this.initializeApp(); // used for an example of ngFor and navigation diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 73e2d49..530e0e8 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -13,6 +13,10 @@ import { SplashScreen } from '@ionic-native/splash-screen'; import { IonicStorageModule } from '@ionic/storage'; import { Device } from '@ionic-native/device'; import { FileChooser } from '@ionic-native/file-chooser'; +import { FilePath } from '@ionic-native/file-path'; +import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer'; +import { File } from '@ionic-native/file'; +import { HTTP } from '@ionic-native/http'; import { TimeagoPipe } from '../pipes/timeago/timeago'; import { ParserPipe } from '../pipes/parser/parser'; @@ -48,6 +52,11 @@ import { ParserPipe } from '../pipes/parser/parser'; SplashScreen, Device, FileChooser, + FilePath, + FileTransfer, + FileTransferObject, + File, + HTTP, {provide: ErrorHandler, useClass: IonicErrorHandler} ] }) diff --git a/src/pages/login/login.ts b/src/pages/login/login.ts index 8e91ba5..8af4c39 100644 --- a/src/pages/login/login.ts +++ b/src/pages/login/login.ts @@ -30,7 +30,7 @@ export class LoginPage { constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage, private device: Device) { - let scope = ['basic','stream','write_post']; + let scope = ['basic','stream','write_post','files']; if (this.device.platform === "Android" || this.device.platform === "amazon-fireos") { this.oauth = new OauthCordova(); diff --git a/src/pages/stream/stream.ts b/src/pages/stream/stream.ts index c1a7ef0..e16deac 100644 --- a/src/pages/stream/stream.ts +++ b/src/pages/stream/stream.ts @@ -3,6 +3,10 @@ import { ViewController, NavController, NavParams, ModalController, Content, Toa import { ThreadPage } from '../thread/thread'; import { Storage } from '@ionic/storage'; import { FileChooser } from '@ionic-native/file-chooser'; +import { FilePath } from '@ionic-native/file-path'; +import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer'; +import { File } from '@ionic-native/file'; +import { HTTP } from '@ionic-native/http'; import { Events } from 'ionic-angular'; import * as pnut from 'pnut-butter'; @@ -338,7 +342,7 @@ export class StreamPage {
{{textCount()}}
@@ -356,13 +360,17 @@ export class NewPostModal { title: string; replyid: string; ptext: string = ""; + fname: string = ""; + fpath: string = ""; options: Object = {}; myUsername: string; + authToken: string; longpost: Object = {}; raw: {type: string, value: Object}[] = []; constructor(public navParams: NavParams, public viewCtrl: ViewController, public toastCtrl: ToastController, - private fileChooser: FileChooser, public events: Events) { + private fileChooser: FileChooser, private storage: Storage, public events: Events, private filePath: FilePath, + private transfer: FileTransfer, private file: File, private http: HTTP) { console.log(JSON.stringify(this.navParams)); this.myUsername = navParams.data.me; if (navParams.data.type === 'reply') { @@ -385,6 +393,9 @@ export class NewPostModal { } else { this.title = "New Post"; } + this.storage.get('token').then((val) => { + this.authToken = val; + }); } dismiss() { @@ -458,9 +469,43 @@ export class NewPostModal { attachImage() { console.log('file chooser'); + const fileTransfer: FileTransferObject = this.transfer.create(); this.fileChooser.open().then(uri => { console.log('File URI: ' + uri); + this.filePath.resolveNativePath(uri).then(filePath => { + this.fpath = filePath; + this.fname = filePath.split('/').pop(); + console.log('-filepath-'); + console.log(this.fpath); + console.log(this.fname); + + let options: FileUploadOptions = { + fileKey: 'content', + fileName: this.fname, + params: { + name: this.fname, + type: 'com.monkeystew.goober_m', + is_public: true + }, + headers: {'Authorization': 'Bearer ' + this.authToken} + } + + fileTransfer.upload(this.fpath, 'https://api.pnut.io/v0/files', options).then((response) => { + console.log('-file created-'); + console.log(JSON.stringify(response)); + console.log('-file created-'); + }).catch((err) => { + console.log('-create file error-'); + console.log(JSON.stringify(err)); + console.log('-create file error-'); + }); + + }).catch(err => { + console.log('-error getting filepath-'); + console.log(err); + }); + }).catch(err => { console.log(err); });