From 1637d534ec10d19a1f43916c57f8279239359d2f Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Fri, 29 May 2020 16:11:33 -0400 Subject: [PATCH] Posting --- .flutter-plugins | 2 + .flutter-plugins-dependencies | 2 +- lib/api/auth.dart | 46 +++++++++++++++++ lib/views/home.dart | 17 ++++++- lib/views/uploadview.dart | 93 +++++++++++++++++++++++++++++++++++ pubspec.yaml | 3 +- 6 files changed, 160 insertions(+), 3 deletions(-) diff --git a/.flutter-plugins b/.flutter-plugins index 96bae77..1508498 100644 --- a/.flutter-plugins +++ b/.flutter-plugins @@ -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/ diff --git a/.flutter-plugins-dependencies b/.flutter-plugins-dependencies index 382f2c3..1e00633 100644 --- a/.flutter-plugins-dependencies +++ b/.flutter-plugins-dependencies @@ -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"} \ No newline at end of file +{"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"} \ No newline at end of file diff --git a/lib/api/auth.dart b/lib/api/auth.dart index 396dbd1..6329819 100644 --- a/lib/api/auth.dart +++ b/lib/api/auth.dart @@ -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 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 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); +} diff --git a/lib/views/home.dart b/lib/views/home.dart index d6734c6..0490382 100644 --- a/lib/views/home.dart +++ b/lib/views/home.dart @@ -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 @@ -109,7 +112,19 @@ class _HomeState extends State { 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())); + } + }); + }, ), ); } diff --git a/lib/views/uploadview.dart b/lib/views/uploadview.dart index e69de29..241533a 100644 --- a/lib/views/uploadview.dart +++ b/lib/views/uploadview.dart @@ -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 { + // 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: [ + 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); + } + }, + ), + ], + )); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 9eea7f7..fb79b9c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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" @@ -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