Send token with every user request

This commit is contained in:
Vishnu Mohandas 2020-04-30 20:48:26 +05:30
parent d228f7278f
commit 16e7e7bec3
7 changed files with 46 additions and 22 deletions

View file

@ -19,6 +19,10 @@ class Configuration {
return preferences.getString(endpointKey);
}
String getHttpEndpoint() {
return "http://" + getEndpoint() + ":8080";
}
void setEndpoint(String endpoint) async {
await preferences.setString(endpointKey, endpoint);
}

View file

@ -1,4 +1,2 @@
const String ENDPOINT = "http://192.168.0.255:8080";
const String USER = "umbu"; // TODO: Fix me
const int THUMBNAIL_SMALL_SIZE = 128;
const int THUMBNAIL_LARGE_SIZE = 512;

View file

@ -1,6 +1,6 @@
import 'package:dio/dio.dart';
import 'package:logger/logger.dart';
import 'package:myapp/core/constants.dart' as Constants;
import 'package:myapp/core/configuration.dart';
import 'package:myapp/db/db_helper.dart';
import 'models/face.dart';
@ -16,8 +16,11 @@ class FaceSearchManager {
Future<List<Face>> getFaces() {
return _dio
.get(Constants.ENDPOINT + "/faces",
queryParameters: {"user": Constants.USER})
.get(Configuration.instance.getHttpEndpoint() + "/faces",
queryParameters: {
"user": Configuration.instance.getUsername(),
"token": Configuration.instance.getToken()
})
.then((response) => (response.data["faces"] as List)
.map((face) => new Face.fromJson(face))
.toList())
@ -26,9 +29,13 @@ class FaceSearchManager {
Future<List<Photo>> getFaceSearchResults(Face face) async {
var futures = _dio.get(
Constants.ENDPOINT + "/search/face/" + face.faceID.toString(),
queryParameters: {"user": Constants.USER}).then((response) => (response
.data["results"] as List)
Configuration.instance.getHttpEndpoint() +
"/search/face/" +
face.faceID.toString(),
queryParameters: {
"user": Configuration.instance.getUsername(),
"token": Configuration.instance.getToken(),
}).then((response) => (response.data["results"] as List)
.map((result) => (DatabaseHelper.instance.getPhotoByPath(result))));
return Future.wait(await futures);
}

View file

@ -13,8 +13,8 @@ import 'package:photo_manager/photo_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:dio/dio.dart';
import 'package:myapp/models/photo.dart';
import 'package:myapp/core/constants.dart' as Constants;
import 'core/configuration.dart';
import 'events/remote_sync_event.dart';
class PhotoSyncManager {
@ -134,7 +134,9 @@ class PhotoSyncManager {
for (Photo photo in diff) {
var localPath = path + basename(photo.remotePath);
await _dio
.download(Constants.ENDPOINT + "/" + photo.remotePath, localPath)
.download(
Configuration.instance.getHttpEndpoint() + "/" + photo.remotePath,
localPath)
.catchError((e) => _logger.e(e));
// TODO: Save path
photo.pathName = localPath;
@ -145,9 +147,11 @@ class PhotoSyncManager {
}
Future<List<Photo>> _getDiff(int lastSyncTimestamp) async {
Response response = await _dio.get(Constants.ENDPOINT + "/diff",
Response response = await _dio.get(
Configuration.instance.getHttpEndpoint() + "/diff",
queryParameters: {
"user": Constants.USER,
"user": Configuration.instance.getUsername(),
"token": Configuration.instance.getToken(),
"lastSyncTimestamp": lastSyncTimestamp
}).catchError((e) => _logger.e(e));
if (response != null) {
@ -165,10 +169,12 @@ class PhotoSyncManager {
var formData = FormData.fromMap({
"file": MultipartFile.fromBytes(await localPhoto.getOriginalBytes()),
"filename": localPhoto.title,
"user": Constants.USER,
"user": Configuration.instance.getUsername(),
"token": Configuration.instance.getToken(),
});
return _dio
.post(Constants.ENDPOINT + "/upload", data: formData)
.post(Configuration.instance.getHttpEndpoint() + "/upload",
data: formData)
.then((response) {
_logger.i(response.toString());
var photo = Photo.fromJson(response.data);
@ -186,10 +192,12 @@ class PhotoSyncManager {
}
Future<void> _deletePhotoOnServer(Photo photo) async {
return _dio.post(Constants.ENDPOINT + "/delete", queryParameters: {
"user": Constants.USER,
"fileID": photo.uploadedFileId
}).catchError((e) => _logger.e(e));
return _dio.post(Configuration.instance.getHttpEndpoint() + "/delete",
queryParameters: {
"user": Configuration.instance.getUsername(),
"token": Configuration.instance.getToken(),
"fileID": photo.uploadedFileId
}).catchError((e) => _logger.e(e));
}
Future _initializeDirectories() async {

View file

@ -1,9 +1,9 @@
import 'package:flutter/material.dart';
import 'package:myapp/core/configuration.dart';
import 'package:myapp/face_search_manager.dart';
import 'package:myapp/models/face.dart';
import 'package:myapp/models/photo.dart';
import 'package:myapp/ui/circular_network_image_widget.dart';
import 'package:myapp/core/constants.dart' as Constants;
import 'package:myapp/ui/thumbnail_widget.dart';
import 'detail_page.dart';
@ -25,7 +25,10 @@ class FaceSearchResultsPage extends StatelessWidget {
Hero(
tag: "face_" + _face.faceID.toString(),
child: CircularNetworkImageWidget(
Constants.ENDPOINT + "/" + _face.thumbnailPath, 20),
Configuration.instance.getHttpEndpoint() +
"/" +
_face.thumbnailPath,
20),
)
],
),

View file

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:myapp/core/configuration.dart';
import 'package:myapp/core/constants.dart' as Constants;
class NetworkImageDetailPage extends StatelessWidget {
@ -33,7 +34,8 @@ class NetworkImageDetailPage extends StatelessWidget {
onVerticalDragUpdate: (details) {
Navigator.pop(context);
},
child: Image.network(Constants.ENDPOINT + "/" + _path),
child:
Image.network(Configuration.instance.getHttpEndpoint() + "/" + _path),
);
}
}

View file

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:myapp/core/configuration.dart';
import 'package:myapp/face_search_manager.dart';
import 'package:myapp/models/face.dart';
import 'package:myapp/core/constants.dart' as Constants;
@ -72,7 +73,8 @@ class SearchPage extends StatelessWidget {
child: Hero(
tag: "face_" + face.faceID.toString(),
child: CircularNetworkImageWidget(
Constants.ENDPOINT + "/" + face.thumbnailPath, 60),
Configuration.instance.getHttpEndpoint() + "/" + face.thumbnailPath,
60),
),
);
}