Created
August 27, 2018 21:59
-
-
Save maxdemarzi/303cb08013921153638cf65527893eb1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<li class="media list-group-item p-4"> | |
<form id="status" enctype="multipart/form-data" action="/post" method="POST" style="width: 100%"> | |
<div class="input-group"> | |
<input name="status" type="text" class="form-control" placeholder="What's happening?" autofocus> | |
<div class="input-group-btn"> | |
<button type="button" class="btn btn-secondary align-self-stretch"> | |
<span class="icon icon-camera"></span> | |
</button> | |
</div> | |
<input name="file" type="file"/> | |
</div> | |
</form> | |
</li> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.maxdemarzi.models; | |
import humanize.Humanize; | |
import lombok.Data; | |
import org.jooby.Upload; | |
import java.time.ZonedDateTime; | |
import java.time.format.DateTimeFormatter; | |
import java.util.Date; | |
@Data | |
public class Post { | |
private Long id; | |
private String status; | |
private String name; | |
private String name2; | |
private String username; | |
private String username2; | |
private String filename; | |
private Upload file; | |
private String hash; | |
private String hash2; | |
private String time; | |
private Integer low_fives; | |
private boolean low_fived; | |
private Integer high_fives; | |
private boolean high_fived; | |
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy"); | |
public String when() { | |
ZonedDateTime dateTime = ZonedDateTime.parse(time); | |
return dateFormat.format(dateTime); | |
} | |
public String humanTime() { | |
return Humanize.naturalTime(Date.from(ZonedDateTime.parse(time).toInstant())); | |
} | |
public String expires() { | |
return Humanize.naturalTime(Date.from(ZonedDateTime.parse(time).plusDays(5).toInstant())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Posts extends Jooby { | |
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss_SSS"); | |
public Posts() { | |
super("posts"); | |
} | |
{ | |
post("/post", req -> { | |
Post post = req.form(Post.class); | |
CommonProfile profile = require(CommonProfile.class); | |
String username = profile.getUsername(); | |
String filename = post.getFilename(); | |
Upload upload = req.file(filename); | |
RequestBody requestBody = RequestBody.create(MultipartBody.FORM, upload.file()); | |
String time = dateFormat.format(ZonedDateTime.now()); | |
Response<ResponseBody> bunnyResponse = App.bunny.upload("fives", username, time, requestBody).execute(); | |
if (bunnyResponse.isSuccessful()) { | |
post.setFilename(username+"/"+time); | |
Response<Post> response = App.api.createPost(username, post).execute(); | |
if (response.isSuccessful()) { | |
return Results.redirect("/home"); | |
} | |
} | |
throw new Err(Status.BAD_REQUEST); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment