file upload to pnut

This commit is contained in:
Morgan McMillian 2018-02-19 07:04:16 -08:00
parent 5026760fa0
commit 50b58cc16f
4 changed files with 59 additions and 4 deletions

View file

@ -3,6 +3,7 @@ import { Nav, Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar'; import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen'; import { SplashScreen } from '@ionic-native/splash-screen';
import { Storage } from '@ionic/storage'; import { Storage } from '@ionic/storage';
import { Device } from '@ionic-native/device';
import { LoginPage } from '../pages/login/login'; import { LoginPage } from '../pages/login/login';
import { StreamPage } from '../pages/stream/stream'; import { StreamPage } from '../pages/stream/stream';
@ -21,7 +22,7 @@ export class MyApp {
pages: Array<{title: string, component: any, params: Object}>; pages: Array<{title: string, component: any, params: Object}>;
constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen, constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen,
private storage: Storage) { private storage: Storage, private device: Device) {
this.initializeApp(); this.initializeApp();
// used for an example of ngFor and navigation // used for an example of ngFor and navigation

View file

@ -13,6 +13,10 @@ import { SplashScreen } from '@ionic-native/splash-screen';
import { IonicStorageModule } from '@ionic/storage'; import { IonicStorageModule } from '@ionic/storage';
import { Device } from '@ionic-native/device'; import { Device } from '@ionic-native/device';
import { FileChooser } from '@ionic-native/file-chooser'; 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 { TimeagoPipe } from '../pipes/timeago/timeago';
import { ParserPipe } from '../pipes/parser/parser'; import { ParserPipe } from '../pipes/parser/parser';
@ -48,6 +52,11 @@ import { ParserPipe } from '../pipes/parser/parser';
SplashScreen, SplashScreen,
Device, Device,
FileChooser, FileChooser,
FilePath,
FileTransfer,
FileTransferObject,
File,
HTTP,
{provide: ErrorHandler, useClass: IonicErrorHandler} {provide: ErrorHandler, useClass: IonicErrorHandler}
] ]
}) })

View file

@ -30,7 +30,7 @@ export class LoginPage {
constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage, constructor(public navCtrl: NavController, public navParams: NavParams, private storage: Storage,
private device: Device) { 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") { if (this.device.platform === "Android" || this.device.platform === "amazon-fireos") {
this.oauth = new OauthCordova(); this.oauth = new OauthCordova();

View file

@ -3,6 +3,10 @@ import { ViewController, NavController, NavParams, ModalController, Content, Toa
import { ThreadPage } from '../thread/thread'; import { ThreadPage } from '../thread/thread';
import { Storage } from '@ionic/storage'; import { Storage } from '@ionic/storage';
import { FileChooser } from '@ionic-native/file-chooser'; 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 { Events } from 'ionic-angular';
import * as pnut from 'pnut-butter'; import * as pnut from 'pnut-butter';
@ -338,7 +342,7 @@ export class StreamPage {
<ion-row justify-content-end> <ion-row justify-content-end>
<ion-col offset-0> <ion-col offset-0>
<button ion-button (click)="attachImage()"> <button ion-button (click)="attachImage()">
<ion-icon name="image"></ion-icon> <ion-icon name="attach"></ion-icon>
</button> </button>
</ion-col> </ion-col>
<ion-col col-2><div text-center>{{textCount()}}</div></ion-col> <ion-col col-2><div text-center>{{textCount()}}</div></ion-col>
@ -356,13 +360,17 @@ export class NewPostModal {
title: string; title: string;
replyid: string; replyid: string;
ptext: string = ""; ptext: string = "";
fname: string = "";
fpath: string = "";
options: Object = {}; options: Object = {};
myUsername: string; myUsername: string;
authToken: string;
longpost: Object = {}; longpost: Object = {};
raw: {type: string, value: Object}[] = []; raw: {type: string, value: Object}[] = [];
constructor(public navParams: NavParams, public viewCtrl: ViewController, public toastCtrl: ToastController, 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)); console.log(JSON.stringify(this.navParams));
this.myUsername = navParams.data.me; this.myUsername = navParams.data.me;
if (navParams.data.type === 'reply') { if (navParams.data.type === 'reply') {
@ -385,6 +393,9 @@ export class NewPostModal {
} else { } else {
this.title = "New Post"; this.title = "New Post";
} }
this.storage.get('token').then((val) => {
this.authToken = val;
});
} }
dismiss() { dismiss() {
@ -458,9 +469,43 @@ export class NewPostModal {
attachImage() { attachImage() {
console.log('file chooser'); console.log('file chooser');
const fileTransfer: FileTransferObject = this.transfer.create();
this.fileChooser.open().then(uri => { this.fileChooser.open().then(uri => {
console.log('File URI: ' + 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 => { }).catch(err => {
console.log(err); console.log(err);
}); });