Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
Posting
Browse files Browse the repository at this point in the history
  • Loading branch information
ewpratten committed May 29, 2020
1 parent a2199f8 commit 1637d53
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .flutter-plugins
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# This is a generated file; do not edit or check into version control.
flutter_plugin_android_lifecycle=/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_plugin_android_lifecycle-1.0.8/
image_picker=/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7/
shared_preferences=/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.7+3/
shared_preferences_macos=/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_macos-0.0.1+9/
shared_preferences_web=/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_web-0.1.2+7/
Expand Down
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"shared_preferences","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.7+3/","dependencies":[]},{"name":"url_launcher","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher-5.4.10/","dependencies":[]}],"android":[{"name":"shared_preferences","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.7+3/","dependencies":[]},{"name":"url_launcher","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher-5.4.10/","dependencies":[]}],"macos":[{"name":"shared_preferences_macos","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_macos-0.0.1+9/","dependencies":[]},{"name":"url_launcher_macos","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-0.0.1+7/","dependencies":[]}],"linux":[],"windows":[],"web":[{"name":"shared_preferences_web","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_web-0.1.2+7/","dependencies":[]},{"name":"url_launcher_web","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_web-0.1.1+6/","dependencies":[]}]},"dependencyGraph":[{"name":"shared_preferences","dependencies":["shared_preferences_macos","shared_preferences_web"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"url_launcher","dependencies":["url_launcher_web","url_launcher_macos"]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]}],"date_created":"2020-05-29 14:52:05.043675","version":"1.17.1"}
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"image_picker","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7/","dependencies":[]},{"name":"shared_preferences","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.7+3/","dependencies":[]},{"name":"url_launcher","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher-5.4.10/","dependencies":[]}],"android":[{"name":"flutter_plugin_android_lifecycle","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_plugin_android_lifecycle-1.0.8/","dependencies":[]},{"name":"image_picker","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/image_picker-0.6.7/","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"shared_preferences","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences-0.5.7+3/","dependencies":[]},{"name":"url_launcher","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher-5.4.10/","dependencies":[]}],"macos":[{"name":"shared_preferences_macos","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_macos-0.0.1+9/","dependencies":[]},{"name":"url_launcher_macos","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-0.0.1+7/","dependencies":[]}],"linux":[],"windows":[],"web":[{"name":"shared_preferences_web","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/shared_preferences_web-0.1.2+7/","dependencies":[]},{"name":"url_launcher_web","path":"/home/ewpratten/development/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_web-0.1.1+6/","dependencies":[]}]},"dependencyGraph":[{"name":"flutter_plugin_android_lifecycle","dependencies":[]},{"name":"image_picker","dependencies":["flutter_plugin_android_lifecycle"]},{"name":"shared_preferences","dependencies":["shared_preferences_macos","shared_preferences_web"]},{"name":"shared_preferences_macos","dependencies":[]},{"name":"shared_preferences_web","dependencies":[]},{"name":"url_launcher","dependencies":["url_launcher_web","url_launcher_macos"]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]}],"date_created":"2020-05-29 16:09:09.719056","version":"1.17.1"}
46 changes: 46 additions & 0 deletions lib/api/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,49 @@ num getMyID() {
// POST https://api.vsbro.co/api/posts/get_post_url {"caption":"text"}
// PUT https://vsbro-photos-prod.s3.amazonaws.com/users/116855/BToGVohzyUdTcuuinTQmQISBvFETyNPo.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAXQCTSMQQGFIGPC6N%2F20200529%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200529T155354Z&X-Amz-Expires=900&X-Amz-SignedHeaders=content-type%3Bhost&X-Amz-Signature=5770951faff8d3440044c20d345cfbfc25700feccbba9276fbff4b5ceaacc8c6
// POST https://api.vsbro.co/api/posts/submit {"caption":"text","filename":"url from past 2 requests"}

void uploadPost(ValueChanged<bool> callback, File f, String caption) async {
// Get token
SharedPreferences prefs = await SharedPreferences.getInstance();
var token = prefs.getString("token");
if (token == null) {
token = "";
}
print(token);

// Post the caption
var response = await http.post("https://api.vsbro.co/api/posts/get_post_url",
body: json.encode({"caption": caption}),
headers: {HttpHeaders.authorizationHeader: "bearer ${token}"});

if (response.statusCode != 200) {
callback(false);
return;
}

// Parse out the response
Map<String, dynamic> host = json.decode(response.body);

// Post the pic
var response2 = await http.put(host["signed_url"],
headers: {HttpHeaders.contentTypeHeader: "image/jpg"},
body: await f.readAsBytes());

if (response2.statusCode != 200) {
print("Failed to upload image");
callback(false);
return;
}

// Finalize the process
var response3 = await http.post("https://api.vsbro.co/api/posts/submit",
body: json.encode({"caption": caption, "filename": host["filename"]}),
headers: {HttpHeaders.authorizationHeader: "bearer ${token}"});

if (response3.statusCode != 200) {
callback(false);
return;
}

callback(true);
}
17 changes: 16 additions & 1 deletion lib/views/home.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:vsbro/api/auth.dart';
import 'package:vsbro/api/feed.dart';
import 'package:vsbro/model/post.dart';
import 'package:vsbro/views/authview.dart';
import 'package:vsbro/views/postitem.dart';
import 'package:vsbro/views/uploadview.dart';

class Home extends StatefulWidget {
@override
Expand Down Expand Up @@ -109,7 +112,19 @@ class _HomeState extends State<Home> {
child: Icon(
Icons.add,
),
onPressed: () {},
onPressed: () {
isUserAuthenticated((t) {
if (t) {
Navigator.push(
context,
MaterialPageRoute(
builder: (coontext) => UploadView()));
} else {
Navigator.push(context,
MaterialPageRoute(builder: (coontext) => AuthView()));
}
});
},
),
);
}
Expand Down
93 changes: 93 additions & 0 deletions lib/views/uploadview.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:vsbro/api/auth.dart';

class UploadView extends StatefulWidget {
UploadView({Key key}) : super(key: key);

final String title = "Upload Post";

@override
_UploadViewState createState() => _UploadViewState();
}

class _UploadViewState extends State<UploadView> {
// Input controllers
TextEditingController captionController = TextEditingController();

File image;

@override
void dispose() {
captionController.dispose();
super.dispose();
}

upload(BuildContext context) {
uploadPost((t) {
if (t) {
Navigator.pop(context);
} else {
setState(() {
this.image = null;
captionController.text = "Failed to upload file";
});
}
}, image, captionController.text);
}

getImage() async {
var tempImage =
await new ImagePicker().getImage(source: ImageSource.gallery);

setState(() {
this.image = File(tempImage.path);
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.pop(context);
},
),
title: Text(widget.title),
centerTitle: true,
),
body: ListView(
children: <Widget>[
SizedBox(height: 40),
Text(
"IMPORTANT: Posting will fail without warning if you have not varified your email. Make sure to verify before posting. Also, there is a large delay after pressing \"post\". Please be patient."),
SizedBox(height: 40),
RaisedButton(
child: Text("Upload Photo"),
onPressed: () {
getImage();
},
),
SizedBox(height: 20),
TextField(
controller: captionController,
decoration: InputDecoration(hintText: "Caption (optional)"),
),
RaisedButton(
child: Text(
(image == null) ? "Upload an image before posting" : "Post"),
onPressed: () {
if (image != null) {
upload(context);
}
},
),
],
));
}
}
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 2.0.1+8
version: 2.0.2+10

environment:
sdk: ">=2.7.0 <3.0.0"
Expand All @@ -25,6 +25,7 @@ dependencies:
url_launcher: ^5.4.2
shared_preferences: '>=0.5.7+3 <2.0.0'
sentry: ">=3.0.0 <4.0.0"
image_picker: ^0.6.7
flutter:
sdk: flutter

Expand Down

0 comments on commit 1637d53

Please sign in to comment.