Skip to content

Commit 79652d5

Browse files
committed
fix examples
1 parent b4a0b20 commit 79652d5

19 files changed

+841
-158
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
.pub/
66

77
build/
8+
9+
node_modules/

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,14 @@ final uploader = FlutterUploader();
122122
#### Create new upload task:
123123

124124
```dart
125-
final uploader = FlutterUploader();
126125
final taskId = await uploader.enqueue(
127126
url: "your upload link", //required: url to upload to
128127
files: [FileItem(filename: filename, savedDir: savedDir, fieldname:"file")], // required: list of files that you want to upload
129128
method: UplaodMethod.POST, // HTTP method (POST or PUT or PATCH)
130129
headers: {"apikey": "api_123456", "userkey": "userkey_123456"},
131130
data: {"name": "john"}, // any data you want to send in upload request
132131
showNotification: false, // send local notification (android only) for upload status
133-
tag: "upload 1"); // headers to be sent in upload request,
132+
tag: "upload 1"); // unique tag for upload task
134133
);
135134
```
136135

android/src/main/java/com/bluechilli/flutteruploader/FlutterUploaderPlugin.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import com.google.gson.Gson;
1313
import com.google.gson.reflect.TypeToken;
1414

15+
import java.lang.reflect.Array;
1516
import java.lang.reflect.Type;
1617
import java.util.ArrayList;
18+
import java.util.Arrays;
1719
import java.util.HashMap;
1820
import java.util.List;
1921
import java.util.Map;
@@ -50,6 +52,8 @@ public class FlutterUploaderPlugin implements MethodCallHandler, Application.Act
5052
private Map<String, String> tasks = new HashMap<>();
5153
private Gson gson = new Gson();
5254
private int taskIdKey = 0;
55+
private final String[] validHttpMethods = new String[] {"POST", "PUT", "PATCH"};
56+
5357
public static void registerWith(Registrar registrar) {
5458
final MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL_NAME);
5559
final FlutterUploaderPlugin plugin = new FlutterUploaderPlugin(registrar, channel);
@@ -61,7 +65,6 @@ private FlutterUploaderPlugin(Registrar registrar, MethodChannel channel) {
6165
this.channel = channel;
6266
this.register = registrar;
6367
this.connectionTimeout = FlutterUploaderInitializer.getConnectionTimeout(registrar.context());
64-
6568
}
6669

6770
private final BroadcastReceiver updateProcessEventReceiver = new BroadcastReceiver() {
@@ -171,6 +174,7 @@ public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
171174
public void onActivityDestroyed(Activity activity) {
172175
}
173176

177+
174178
private void enqueue(MethodCall call, MethodChannel.Result result) {
175179
taskIdKey++;
176180
String url = call.argument("url");
@@ -181,12 +185,13 @@ private void enqueue(MethodCall call, MethodChannel.Result result) {
181185
boolean showNotification = call.argument("show_notification");
182186
String tag = call.argument("tag");
183187

188+
List<String> methods = Arrays.asList(validHttpMethods);
184189

185190
if(method == null) {
186191
method = "POST";
187192
}
188193

189-
if(method != null && (method.toUpperCase() != "POST" || method.toUpperCase() != "PUT" || method.toUpperCase() != "PATCH" )) {
194+
if(!methods.contains(method.toUpperCase())) {
190195
result.error("invalid_method", "Method must be either POST | PUT | PATCH", null);
191196
return;
192197
}

example/README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,28 @@ Demonstrates how to use the flutter_uploader plugin.
44

55
## Getting Started
66

7-
For help getting started with Flutter, view our online
8-
[documentation](https://flutter.io/).
7+
# Setup upload Api
8+
9+
1. install firebase-tools in terminal
10+
11+
```console
12+
npm install -g firebase-tools
13+
```
14+
15+
2. create project in firebase console
16+
17+
3. login to firebase in terminal
18+
19+
```console
20+
firebase login
21+
```
22+
23+
4. Go to example/backend/
24+
25+
5. run
26+
27+
```console
28+
firebase deploy
29+
```
30+
31+
6. run example app

example/backend/firebase.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"functions": {
3+
"predeploy": [
4+
"npm --prefix \"$RESOURCE_DIR\" run lint"
5+
],
6+
"source": "functions"
7+
}
8+
}
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"parserOptions": {
3+
// Required for certain syntax usages
4+
"ecmaVersion": 6
5+
},
6+
"plugins": [
7+
"promise"
8+
],
9+
"extends": "eslint:recommended",
10+
"rules": {
11+
// Removed rule "disallow the use of console" from recommended eslint rules
12+
"no-console": "off",
13+
14+
// Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
15+
"no-regex-spaces": "off",
16+
17+
// Removed rule "disallow the use of debugger" from recommended eslint rules
18+
"no-debugger": "off",
19+
20+
// Removed rule "disallow unused variables" from recommended eslint rules
21+
"no-unused-vars": "off",
22+
23+
// Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
24+
"no-mixed-spaces-and-tabs": "off",
25+
26+
// Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
27+
"no-undef": "off",
28+
29+
// Warn against template literal placeholder syntax in regular strings
30+
"no-template-curly-in-string": 1,
31+
32+
// Warn if return statements do not either always or never specify values
33+
"consistent-return": 1,
34+
35+
// Warn if no return statements in callbacks of array methods
36+
"array-callback-return": 1,
37+
38+
// Require the use of === and !==
39+
"eqeqeq": 2,
40+
41+
// Disallow the use of alert, confirm, and prompt
42+
"no-alert": 2,
43+
44+
// Disallow the use of arguments.caller or arguments.callee
45+
"no-caller": 2,
46+
47+
// Disallow null comparisons without type-checking operators
48+
"no-eq-null": 2,
49+
50+
// Disallow the use of eval()
51+
"no-eval": 2,
52+
53+
// Warn against extending native types
54+
"no-extend-native": 1,
55+
56+
// Warn against unnecessary calls to .bind()
57+
"no-extra-bind": 1,
58+
59+
// Warn against unnecessary labels
60+
"no-extra-label": 1,
61+
62+
// Disallow leading or trailing decimal points in numeric literals
63+
"no-floating-decimal": 2,
64+
65+
// Warn against shorthand type conversions
66+
"no-implicit-coercion": 1,
67+
68+
// Warn against function declarations and expressions inside loop statements
69+
"no-loop-func": 1,
70+
71+
// Disallow new operators with the Function object
72+
"no-new-func": 2,
73+
74+
// Warn against new operators with the String, Number, and Boolean objects
75+
"no-new-wrappers": 1,
76+
77+
// Disallow throwing literals as exceptions
78+
"no-throw-literal": 2,
79+
80+
// Require using Error objects as Promise rejection reasons
81+
"prefer-promise-reject-errors": 2,
82+
83+
// Enforce “for” loop update clause moving the counter in the right direction
84+
"for-direction": 2,
85+
86+
// Enforce return statements in getters
87+
"getter-return": 2,
88+
89+
// Disallow await inside of loops
90+
"no-await-in-loop": 2,
91+
92+
// Disallow comparing against -0
93+
"no-compare-neg-zero": 2,
94+
95+
// Warn against catch clause parameters from shadowing variables in the outer scope
96+
"no-catch-shadow": 1,
97+
98+
// Disallow identifiers from shadowing restricted names
99+
"no-shadow-restricted-names": 2,
100+
101+
// Enforce return statements in callbacks of array methods
102+
"callback-return": 2,
103+
104+
// Require error handling in callbacks
105+
"handle-callback-err": 2,
106+
107+
// Warn against string concatenation with __dirname and __filename
108+
"no-path-concat": 1,
109+
110+
// Prefer using arrow functions for callbacks
111+
"prefer-arrow-callback": 1,
112+
113+
// Return inside each then() to create readable and reusable Promise chains.
114+
// Forces developers to return console logs and http calls in promises.
115+
"promise/always-return": 2,
116+
117+
//Enforces the use of catch() on un-returned promises
118+
"promise/catch-or-return": 2,
119+
120+
// Warn against nested then() or catch() statements
121+
"promise/no-nesting": 1
122+
}
123+
}

example/backend/functions/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

example/backend/functions/index.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const path = require("path");
2+
const os = require("os");
3+
const fs = require("fs");
4+
const functions = require("firebase-functions");
5+
const cors = require("cors")({ origin: true });
6+
const Busboy = require("busboy");
7+
8+
exports.upload = functions.https.onRequest((req, res) => {
9+
cors(req, res, () => {
10+
const uploads = [];
11+
const methods = ["POST", "PUT", "PATCH"];
12+
if (!methods.includes(req.method, 0)) {
13+
return res.status(405).json({
14+
errorMessage: "Method is not allowed"
15+
});
16+
}
17+
18+
const busboy = new Busboy({ headers: req.headers });
19+
20+
busboy.on("file", (fieldname, file, filename, encoding, mimetype) => {
21+
console.log("File [" + fieldname + "]: filename: " + filename);
22+
file.on("data", data => {
23+
console.log("File [" + fieldname + "] got " + data.length + " bytes");
24+
});
25+
file.on("end", () => {
26+
console.log("File [" + fieldname + "] Finished");
27+
});
28+
29+
const filepath = path.join(os.tmpdir(), filename);
30+
uploads.push({
31+
file: filepath,
32+
fieldname: fieldname,
33+
filename: filename,
34+
mimetype: mimetype,
35+
encoding: encoding
36+
});
37+
console.log(`Saving '${fieldname}' to ${filepath}`);
38+
file.pipe(fs.createWriteStream(filepath));
39+
});
40+
busboy.on("field", (fieldname, val, fieldnameTruncated, valTruncated) => {
41+
console.log("Field [" + fieldname + "]: value: " + val);
42+
});
43+
busboy.on("finish", () => {
44+
res.status(200).json({
45+
message: "Successfully uploaded"
46+
});
47+
res.end();
48+
});
49+
busboy.on("error", error => {
50+
res.status(500).json({
51+
errorMessage: error.toString(),
52+
error: error
53+
});
54+
res.end();
55+
});
56+
busboy.end(req.rawBody);
57+
req.pipe(busboy);
58+
});
59+
});
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "functions",
3+
"description": "Cloud Functions for Firebase",
4+
"scripts": {
5+
"lint": "eslint .",
6+
"serve": "firebase serve --only functions",
7+
"shell": "firebase functions:shell",
8+
"start": "npm run shell",
9+
"deploy": "firebase deploy --only functions",
10+
"logs": "firebase functions:log"
11+
},
12+
"dependencies": {
13+
"aws-sdk": "^2.418.0",
14+
"busboy": "^0.3.0",
15+
"cors": "^2.8.5",
16+
"firebase-admin": "^7.0.0",
17+
"firebase-functions": "^2.2.0"
18+
},
19+
"devDependencies": {
20+
"eslint": "^5.12.0",
21+
"eslint-plugin-promise": "^4.0.1"
22+
},
23+
"private": true
24+
}

0 commit comments

Comments
 (0)