File upload support

parent 80c66470
......@@ -4,6 +4,7 @@ import co.melondev.cubedpay.data.*;
import co.melondev.cubedpay.data.common.Cursor;
import retrofit2.http.*;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
......@@ -19,6 +20,9 @@ public interface CubedPayShopAPI {
@POST("/shop/{sid}")
CompletableFuture<Shop> updateShop(@Path("sid") String shopId, @Query("name") String name, @Query("sandbox") boolean sandbox);
@POST("/shop/{sid}")
CompletableFuture<Shop> updateShop(@Path("sid") String shopId, @Query("name") String name, @Query("sandbox") boolean sandbox, @Query("logo") File logo);
@GET("/shop/{sid}/customers")
CompletableFuture<PublicUser> getCustomers(@Path("sid") String shopId);
......@@ -46,9 +50,15 @@ public interface CubedPayShopAPI {
@POST("/shop/{sid}/package")
CompletableFuture<ShopPackage> createPackage(@Path("sid") String shopId, @Query("name") String name, @Query("description") String description, @Query("price") double price, @Query("public") boolean isPublic);
@POST("/shop/{sid}/package")
CompletableFuture<ShopPackage> createPackage(@Path("sid") String shopId, @Query("name") String name, @Query("description") String description, @Query("price") double price, @Query("public") boolean isPublic, @Query("icon") File icon);
@PATCH("/shop/{sid}/package/{pid}")
CompletableFuture<ShopPackage> updatePackage(@Path("sid") String shopId, @Path("pid") String packageId, @Query("name") String name, @Query("description") String description, @Query("price") double price, @Query("public") boolean isPublic);
@PATCH("/shop/{sid}/package/{pid}")
CompletableFuture<ShopPackage> updatePackage(@Path("sid") String shopId, @Path("pid") String packageId, @Query("name") String name, @Query("description") String description, @Query("price") double price, @Query("public") boolean isPublic, @Query("icon") File icon);
@DELETE("/shop/{sid}/package/{pid}")
CompletableFuture<DeleteConfirmation> deletePackage(@Path("sid") String shopId, @Path("pid") String packageId);
//endregion
......@@ -60,12 +70,12 @@ public interface CubedPayShopAPI {
@GET("/shop/{sid}/transaction/{tid}")
CompletableFuture<Transaction> getTransaction(@Path("sid") String shopId, @Path("tid") String transactionId);
default CompletableFuture<Transaction> createTransaction(String shopId, String customer, Item... item) {
return createTransaction(shopId, customer, new Items(item));
default CompletableFuture<Transaction> createTransaction(String shopId, String customer, String profile, Item... item) {
return createTransaction(shopId, customer, profile, new Items(item));
}
@POST("/shop/{sid}/transaction")
CompletableFuture<Transaction> createTransaction(@Path("sid") String shopId, @Query("customer") String customer, @Body Items items);
CompletableFuture<Transaction> createTransaction(@Path("sid") String shopId, @Query("customer") String customer, @Query("profile") String profile, @Body Items items);
@PUT("/shop/{sid}/transaction/{tid}/discount")
CompletableFuture<Transaction> setTransactionDiscount(@Path("sid") String shopId, @Path("tid") String transactionId, @Query("discount") String discountCode);
......
......@@ -7,8 +7,14 @@ import retrofit2.Converter;
import retrofit2.Converter.Factory;
import retrofit2.Retrofit;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URLConnection;
import java.util.Base64;
public class APIEnvelopeTransformerConverterFactory extends Factory {
......@@ -37,6 +43,39 @@ public class APIEnvelopeTransformerConverterFactory extends Factory {
if (type instanceof Class && ((Class) type).isEnum()) {
return (Converter<Object, String>) value -> ((Enum) value).name();
}
if (type instanceof Class && ((Class) type) == File.class) {
return (Converter<Object, String>) value -> {
File file = (File) value;
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
long length = file.length();
if (length > Integer.MAX_VALUE) {
throw new IOException("File is too large to be sent");
}
byte[] bytes = new byte[(int) length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file " + file.getName());
}
in.close();
//I know what your thinking, but this is how I got things to work
in = new BufferedInputStream(new FileInputStream(file));
String mimeType = URLConnection.guessContentTypeFromStream(in);
in.close();
if (!mimeType.toLowerCase().startsWith("image/")) {
throw new IOException("Only can upload image files");
}
return "data:" + mimeType + ";base64," + Base64.getEncoder().encodeToString(bytes);
};
}
return gson::toJson;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment