Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PartMap should add filename to each file part #1293

Closed
wesleywu opened this issue Nov 17, 2015 · 2 comments
Closed

PartMap should add filename to each file part #1293

wesleywu opened this issue Nov 17, 2015 · 2 comments

Comments

@wesleywu
Copy link

PartMap should add filename to each file part, because many Java servlet container treat part as an upload file only by the filename attribute.

Also, the Content-Transfer-Encoding header is useless.

I hacked the PartMap impl

static final class PartMap extends RequestBuilderAction {
  private final Retrofit retrofit;
  private final String transferEncoding;
  private final Annotation[] annotations;

  PartMap(Retrofit retrofit, String transferEncoding, Annotation[] annotations) {
    this.retrofit = retrofit;
    this.transferEncoding = transferEncoding;
    this.annotations = annotations;
  }

  @Override void perform(RequestBuilder builder, Object value) {
    if (value == null) return; // Skip null values.

    Map<?, ?> map = (Map<?, ?>) value;
    for (Map.Entry<?, ?> entry : map.entrySet()) {
      Object entryKey = entry.getKey();
      if (entryKey == null) {
        throw new IllegalArgumentException("Part map contained null key.");
      }
      Object entryValue = entry.getValue();
      if (entryValue == null) {
        continue; // Skip null values.
      }

      // hack by wesley wu start
      // support multivaluemap
      if (entryValue instanceof Collection) {
        Collection valueCollection = (Collection) entryValue;
        for (Object valueOne : valueCollection) {
          addPart(builder, entryKey, valueOne);
        }
      } else {
        addPart(builder, entryKey, entryValue);
      }
      // hack by wesley wu end
    }
  }

  private void addPart(RequestBuilder builder, Object entryKey, Object entryValue) {
    Headers headers;

    Class<?> entryClass = entryValue.getClass();
    //noinspection unchecked
    Converter<Object, RequestBody> converter =
            retrofit.requestConverter(entryClass, annotations);
    RequestBody body;
    try {
      body = converter.convert(entryValue);
    } catch (IOException e) {
      throw new RuntimeException("Unable to convert " + entryValue + " to RequestBody", e);
    }
    // dirty hack by wesley wu start
    if (BINARY_TYPE.contains(body.contentType().type())) {
      headers = Headers.of(
              "Content-Disposition", "form-data; name=\"" + entryKey + "\"; filename=\"" + entryKey + "\"",
              "Content-Transfer-Encoding", transferEncoding);
    } else {
      headers = Headers.of(
              "Content-Disposition", "form-data; name=\"" + entryKey + "\"");
    }
    // dirty hack by wesley wu end
    builder.addPart(headers, body);
  }
}
@wesleywu
Copy link
Author

duplicated with #1140. Please ignore this :)

@JakeWharton
Copy link
Collaborator

Dupe #1140.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants