Processing an Image in Dart

677 views Asked by At

I am currently trying to process an image on the server that was uploaded by a user. My goal is to take this image that was uploaded, process it on the server and then upload it to rackspace cloud files. I have had no luck in finding a way. I am hoping that someone can lead me into the correct direction.

Heres what I have so far on the server side for processing

Future < Null > handleUploadRequest(final HttpRequest httpRequest) async {
  print('handle upload -------------------------');

  var data;

  await httpRequest.fold(new BytesBuilder(), (b, d) => b..add(d)).then((builder) {
    data = builder.takeBytes();

    String encodedData = JSON.encode(data);

    int dataLength = encodedData.length;

    // Uploading image to rackspace cloud files

   // Here we have to send a request with username & api key to get an auth token
http.post(tokens, headers : {'Content-Type':'application/json'}, body: JSON.encode({"auth": {"RAX-KSKEY:apiKeyCredentials":{"username":"XXXXXXXXXXXXXXX","apiKey":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"}}})).then((response) {
   print("Response status: ${response.statusCode}");
   print("Response body: ${response.body}");
   return response;
}).then((response){
   authResponse = JSON.decode(response.body);
   String token = authResponse['access']['token']['id'];


/////////// This is where we upload the image to Rackspace ///////

  var request = new http.MultipartRequest("PUT", Uri.parse('https://storage101.iad3.clouddrive.com/v1/MossoCloudFS_XXXXXXX/TestContainer/testimage.jpg'));
  request.headers['X-Auth-Token'] = token;
  request.headers['Content-Type'] = 'image/jpeg';

  var stream = new http.ByteStream.fromBytes(data);

  request.files.add(new http.MultipartFile('file', stream, data.length, filename: 'testfile.jpg', contentType: 'image/jpeg');
  print('requesT: ${request}');
  request.send().then((response) {
   print(response.statusCode);
   print(response.headers);
   print(response.stream);
   if (response.statusCode == 200) print("Uploaded!");
  });


//// End of Upload to Rackspace //////////

    print('Upload Complete!');
    httpRequest.response.write(data);
    await httpRequest.response.close();
}

The only issue right now is that in https://pub.dartlang.org/packages/http, I need to call the type MediaType in the parameter content-type. I have no idea how to call this. It seems like it is a factory inside a class? If I do not call a content-type then it defaults to octet-stream which cannot be opened from the cdn storage container.

Reference to this way of uploading is from How can I upload a PDF using Dart's HttpClient?

1

There are 1 answers

18
Günter Zöchbauer On

Looks like an missing return before http.put(...) and httpRequest.response.close();

Either you use await before each async call (call to functions which return a Future) or return each such Future to the caller to preserve order of execution. Without any of these the async execution is scheduled for later execution and the next line of your code is executed instead before the async code you called even started.

Update pure async/await implementation

import 'package:http/http.dart' as http;
import 'dart:async' show Future, Stream;
import 'dart:io';

Future<Null> handleUploadRequest(final HttpRequest httpRequest) async {
  print('handle upload -------------------------');
  print('httpRequest: ${httpRequest.headers}');

  var data;

  var builder = await httpRequest.fold(new BytesBuilder(), (b, d) => b..add(d));
  data = builder.takeBytes();
  print('bytes builder: ${data}');

  // Uploading image to rackspace cloud files
  var url =
      'https://storage101.dfw1.clouddrive.com/v1/{accountnumber}/{container}/';
  var response = await http.put(url,
      headers: {
        'X-Auth-Token': '{XXXXXXXXXAPI_KEYXXXXXXXXXX}',
        'Content-Type': 'image/jpeg',
        'Content-Length': '${httpRequest.headers.contentLength}',
      },
      body: data);

  print("Response status: ${response.statusCode}");
  print("Response body: ${response.body}");

  print('closing connection with data complete');
  httpRequest.response.write(data);
  await httpRequest.response.close();
}