Skip to content

Commit

Permalink
feat: display image
Browse files Browse the repository at this point in the history
  • Loading branch information
gutche committed Apr 22, 2023
1 parent a5f57b4 commit 3d148bc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import net.daw.alist.models.User;
import net.daw.alist.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -148,8 +152,8 @@ public ResponseEntity<ArrayList> downvotePost(Authentication authentication, @Pa
Optional<Post> optionalPost = postService.findByID(postId);
if (optionalPost.isPresent()) {
User userSession = (User) userService.loadUserByUsername(((User) authentication
.getPrincipal())
.getUsername());
.getPrincipal())
.getUsername());
votesService.actionDownVote(postId, userSession);
ArrayList<Integer> upvotesAndDownvotes = new ArrayList<>();
upvotesAndDownvotes.add(optionalPost.get().getNumUpvotes());
Expand All @@ -158,6 +162,18 @@ public ResponseEntity<ArrayList> downvotePost(Authentication authentication, @Pa
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@GetMapping("/images/{id}")
public ResponseEntity<Object> downloadImage(@PathVariable long id) throws SQLException {
Optional<PostItem> postItem = postItemService.findById(id);

if (postItem.isPresent() && postItem.get().getImage() != null) {
Resource file = new InputStreamResource(postItem.get().getImage().getBinaryStream());
return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, "image/jpeg").contentLength(postItem.get().getImage().length()).body(file);
} else {
return ResponseEntity.notFound().build();
}
}

@AllArgsConstructor
@Getter
Expand Down
2 changes: 1 addition & 1 deletion front/src/app/post/components/post/post.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ <h1 class="topName">{{post.title}}</h1>
<li class="ms-0 me-auto">
{{item.description}}
</li>
<img src="{{item.id}}/image" alt="image" width="45" height="45"
<img [src]="fetchImage(item)" alt="image" width="45" height="45"
class="itemImage rounded-circle ms-auto me-2 mt-auto mb-auto">
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions front/src/app/post/components/post/post.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Post } from 'src/app/models/post.model';
import { PostsService } from '../../services/posts.service';
import { distinctUntilChanged } from 'rxjs';
import { AuthService } from 'src/app/auth/services/auth.service';
import { PostItem } from 'src/app/models/postItem.model';

@Component({
selector: 'app-post',
Expand Down Expand Up @@ -80,6 +81,10 @@ export class PostComponent implements OnInit {
if (downvotes !== undefined) this.post.numDownvotes = downvotes;
}

fetchImage(postItem: PostItem) {
return this.postsService.downloadImage(postItem);
}

//TODO: UPVOTES AND DOWNVOTES
//upvoted: boolean = this.post.upVotes.find;
//downvoted: boolean = this.post.downVotes.
Expand Down
5 changes: 5 additions & 0 deletions front/src/app/post/services/posts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { Injectable } from '@angular/core';
import { Observable, map, filter, BehaviorSubject } from 'rxjs';
import { Post } from 'src/app/models/post.model';
import { PostItem } from 'src/app/models/postItem.model';

const BASE_URL = 'api/posts';

Expand Down Expand Up @@ -40,4 +41,8 @@ export class PostsService {
getFilter() {
return this.filter.asObservable();
}

downloadImage(postItem: PostItem): String {
return BASE_URL + '/images/' + postItem.id;
}
}

0 comments on commit 3d148bc

Please sign in to comment.