Skip to content

Commit 0e2ff8c

Browse files
committed
tried to load poster imgs via backend server
1 parent 9cd103f commit 0e2ff8c

File tree

7 files changed

+82
-244
lines changed

7 files changed

+82
-244
lines changed

notes - keywords fetched.txt

-5
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@ movie 926391
1818
movie 927208
1919
movie 939342
2020
movie 1174218
21-
tv 135670
2221

2322
SELECT childtype, tmdb_id, title, original_title FROM servie
2423
WHERE (childtype = 'movie' AND tmdb_id IN (19263,34384,277090,327796,499466,532805,568672,631969,827669,836529,861345,912502,926390,926391,927208,939342,1174218))
25-
OR (childtype = 'tv' AND tmdb_id = 135670);
26-
27-
28-
to delete dhindora - 135670, 274593
2924

3025
239/2266 -> 0 counts
3126

notes - posterImg quck loading.txt

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Whether serving the images locally from your backend will be faster depends on several factors, including the speed of the external server from which the images are being fetched, the network latency, and the optimization of your backend server. Here's a breakdown to help you decide:
2+
3+
1. External Server Latency:
4+
If the images are hosted on a third-party server, there's a chance that the latency of fetching the images from that server could be contributing to the delay. The time it takes for your frontend to make a request and receive a response can be significant, especially if the images are large or if the server is geographically distant.
5+
By downloading the images and serving them from your backend, you bypass this external latency, and the frontend would load the images directly from your server, reducing the round-trip time.
6+
2. Local Server Performance:
7+
Your backend server will likely be closer to your React application (especially if both are running in the same data center or cloud region), which can reduce network latency compared to fetching images from an external server.
8+
However, the backend's ability to serve static files (like images) quickly depends on its hardware and how well your static file serving is configured. If the images are served through an optimized static file server or CDN (Content Delivery Network), it can be faster than downloading them one by one from an external source.
9+
3. Caching Mechanism:
10+
One of the most significant advantages of serving images locally is the ability to cache them both on your backend and on the client side. For instance, if you serve the images via a CDN or use proper caching headers, browsers can cache images, reducing the load time on subsequent requests.
11+
If you download the images once and serve them locally, the first load might take some time (depending on the size and number of images), but subsequent loads of the same images will be much faster due to caching.
12+
4. Image Optimization:
13+
Ensure the images are optimized before serving them. For example, you can compress the images, convert them to modern formats like WebP, or resize them to reduce their file size. This can significantly reduce the time it takes to load the images.
14+
5. Asynchronous Loading:
15+
Even if serving from the backend is faster, consider implementing asynchronous loading or lazy loading of images on the frontend. This way, images load progressively as the user scrolls down the page, making the experience feel faster.
16+
Will It Be Faster?
17+
If the external server has a high latency or is slow to serve the images, serving them from your backend could be significantly faster.
18+
However, if the external server is already fast and the bottleneck is elsewhere (e.g., image size or frontend rendering), serving the images locally may not result in a dramatic improvement.
19+
The first load will likely take longer because of the image download process, but once the images are cached locally on your backend and/or on the client, subsequent loads will be faster.
20+
Recommendation:
21+
Benchmark: Run some performance tests to compare the time taken to load images from the external source vs. serving them locally.
22+
Use a CDN: If possible, use a CDN to serve the images. A CDN will cache the images closer to the user's location, drastically reducing load times.
23+
Optimize Images: Compress and optimize the images to reduce their size, which will improve load times, whether they're served from an external server or your backend.
24+
Lazy Loading: Implement lazy loading on the frontend to only load images when they enter the viewport.
25+
By combining these strategies, you should be able to achieve a noticeable improvement in image load times.

src/main/java/servie/track_servie/config/ApiSecurity.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
3232
"/track-servie/auth/login",
3333
"/track-servie/react/**", // temporarily
3434
"/track-servie/user/image/**",
35-
"/profilePics/**")
35+
"/profilePics/**",
36+
"/posterImgs/**",
37+
"/staticPosterImgs/**")
3638
.permitAll()
3739
.anyRequest()
3840
.authenticated())

src/main/java/servie/track_servie/config/WebConfig.java

+4
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ public void addResourceHandlers(ResourceHandlerRegistry registry)
2323
{
2424
registry.addResourceHandler("/profilePics/**")
2525
.addResourceLocations("file:/home/aakkiieezz/Coding/track_servie/profilePics/");
26+
// registry.addResourceHandler("/posterImgs/**")
27+
// .addResourceLocations("file:/home/aakkiieezz/Coding/track_servie/posterImgs/");
28+
registry.addResourceHandler("/staticPosterImgs/**")
29+
.addResourceLocations("classpath:/static/staticPosterImgs/");
2630
}
2731
}

src/main/java/servie/track_servie/service/ImageService.java

+50
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package servie.track_servie.service;
22

3+
import java.io.InputStream;
4+
import java.nio.file.Path;
35
import java.util.ArrayList;
46
import java.util.List;
57
import org.springframework.beans.factory.annotation.Autowired;
@@ -8,6 +10,7 @@
810
import org.springframework.http.HttpMethod;
911
import org.springframework.http.HttpStatus;
1012
import org.springframework.http.ResponseEntity;
13+
import org.springframework.scheduling.annotation.Scheduled;
1114
import org.springframework.stereotype.Service;
1215
import org.springframework.web.client.HttpClientErrorException;
1316
import org.springframework.web.client.RestTemplate;
@@ -21,6 +24,11 @@
2124
import servie.track_servie.exceptions.TmdbApiException;
2225
import servie.track_servie.payload.dtos.TmdbError;
2326
import servie.track_servie.repository.ServieRepository;
27+
import java.io.IOException;
28+
import java.io.InputStream;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import java.util.List;
2432

2533
@Service
2634
public class ImageService
@@ -31,6 +39,7 @@ public class ImageService
3139
private RestTemplate restTemplate;
3240
@Value("${tmdb.api.base-url}")
3341
private String tmdbApiBaseUrl;
42+
private static final String directoryPosterImgs = "/home/aakkiieezz/Coding/track_servie/posterImgs/";
3443
// private HttpHeaders headers = new HttpHeaders();
3544
// private HttpEntity<?> httpEntity = new HttpEntity<>(headers, (MultiValueMap<String, String>) null);
3645

@@ -136,6 +145,47 @@ public TmdbImageDto getImages(Integer tmdbId, String childType) throws JsonProce
136145
throw new RuntimeException("Failed to fetch data from TMDB", e);
137146
}
138147
}
148+
149+
// @Scheduled(fixedRate = Integer.MAX_VALUE)
150+
public void downloadImages()
151+
{
152+
List<String> imageUrls = servieRepository.findAll().stream()
153+
.map(s -> s.getPosterPath())
154+
.toList();
155+
Path outputDirectory = Path.of(directoryPosterImgs);
156+
try
157+
{
158+
Files.createDirectories(outputDirectory);
159+
for(String imageUrl : imageUrls)
160+
downloadImage("https://image.tmdb.org/t/p/original"+imageUrl, outputDirectory);
161+
}
162+
catch(Exception e)
163+
{
164+
e.printStackTrace();
165+
}
166+
}
167+
168+
private void downloadImage(String imageUrl, Path outputDirectory)
169+
{
170+
try
171+
{
172+
ResponseEntity<byte[]> response = restTemplate.getForEntity(imageUrl, byte[].class);
173+
if(response.getStatusCode().is2xxSuccessful() && response.getBody()!=null)
174+
{
175+
String fileName = Path.of(imageUrl).getFileName().toString();
176+
Path outputPath = outputDirectory.resolve(fileName);
177+
Files.write(outputPath, response.getBody());
178+
System.out.println("Downloaded: "+fileName);
179+
}
180+
else
181+
System.err.println("Failed to download: "+imageUrl+" (HTTP "+response.getStatusCode()+")");
182+
}
183+
catch(Exception e)
184+
{
185+
System.err.println("Error downloading image: "+imageUrl);
186+
e.printStackTrace();
187+
}
188+
}
139189
}
140190
// https://api.themoviedb.org/3/movie/11/images
141191
// https://api.themoviedb.org/3/movie/{movie_id}/images
-6.89 KB
Binary file not shown.

src/main/resources/static/css/mystyles.css

-238
This file was deleted.

0 commit comments

Comments
 (0)