-
Notifications
You must be signed in to change notification settings - Fork 0
Implement-file-streaming-for-large-files-#68 #91
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
Open
Boppler12
wants to merge
28
commits into
main
Choose a base branch
from
Implement-file-streaming-for-large-files-#68
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
b9600f6
ska vara klart nu har glömt att commit jobbet inser jag dock
Boppler12 07e47bf
har lagt till i line 20-25 samt line 29
Boppler12 8e0ab50
tester för FileCacheTest
Boppler12 fffdebf
updaterat i StaticFileHandler och skapat CacheFilter
Boppler12 6df1039
code funkar ska lägga till tester till StaticFileHandler
Boppler12 8f240f9
lagt till en webrott som behövs för testerna jag jobbar på =)
Boppler12 cb5e5ad
lagt till tester för StaticFileHandler
Boppler12 c79c1a6
förbättrat FileCache med LRU-policy och tråd-säkerhet, lagt till bätt…
Boppler12 1c57ca3
Fixar problem
Boppler12 919be74
Feature/13 implement config file (#22)
MartinStenhagen a8868e0
Enhancement/404 page not found (#53)
codebyNorthsteep 21079fb
Feature/issue59 run configloader (#61)
MartinStenhagen d121cfa
23 define and create filter interface (#46)
eraiicphu ad9e1c4
Feature/mime type detection #8 (#47)
gitnes94 d12df61
Dockerfile update (#52) (#63)
Xeutos 5514dfc
Added comprehensive README.MD (#67)
gitnes94 4bc2b13
Fix: Path traversal vulnerability in StaticFileHandler (#65)
apaegs 34febcd
Resolve port: CLI > config > default (#29)
viktorlindell12 64742ca
Refactor status codes to constants #71 (#77)
eeebbaandersson 4b79dd4
fixed file path (#86)
gurkvatten 1231b1a
Fix path in Dockerfile for `www` directory copy operation (#87)
codebyNorthsteep 304a0e9
Feature/27 ipfilter (#70)
apaegs 94630dc
Feature/LocaleFilter (#81)
AntonAhlqvist 654449d
updaterat i StaticFileHandler och skapat CacheFilter
Boppler12 028bf60
förbättrat FileCache med LRU-policy och tråd-säkerhet, lagt till bätt…
Boppler12 b2f045a
förbättrat StaticFileHandler med fil-strömning för stora filer, minne…
Boppler12 754e7fe
förbättrat HttpResponseBuilder med stöd för header-bygge, optimerad c…
Boppler12 e8781bd
Merge branch 'main' into Implement-file-streaming-for-large-files-#68
Boppler12 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package org.example; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| public class CacheFilter { | ||
| private final FileCache cache = new FileCache(); | ||
|
|
||
| public byte[] getOrFetch(String uri, FileProvider provider) throws IOException { | ||
| Objects.requireNonNull(uri, "URI kan inte vara null"); | ||
| Objects.requireNonNull(provider, "Provider kan inte vara null"); | ||
|
|
||
| // Atomic operation: get() returnerar null om entry inte finns | ||
| byte[] cached = cache.get(uri); | ||
|
|
||
| if (cached != null) { | ||
| logCacheHit(uri); | ||
| return cached; | ||
| } | ||
|
|
||
| logCacheMiss(uri); | ||
| byte[] fileBytes = provider.fetch(uri); | ||
|
|
||
| if (fileBytes != null) { | ||
| cache.put(uri, fileBytes); | ||
| } | ||
| return fileBytes; | ||
| } | ||
|
|
||
| private void logCacheHit(String uri) { | ||
| System.out.println("Cache hit for: " + uri); | ||
| } | ||
|
|
||
| private void logCacheMiss(String uri) { | ||
| System.out.println("Cache miss for: " + uri); | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| public interface FileProvider { | ||
| byte[] fetch(String uri) throws IOException; | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
|
|
||
| package org.example; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.locks.ReadWriteLock; | ||
| import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
|
|
||
| /** | ||
| * Thread-safe file cache med LRU eviction policy. | ||
| * Lagrar upp till MAX_CACHE_SIZE bytes totalt. | ||
| */ | ||
| public class FileCache { | ||
| private static final long MAX_CACHE_SIZE = 50 * 1024 * 1024; // 50MB | ||
| private static final int MAX_ENTRIES = 1000; | ||
|
|
||
| private final Map<String, CacheEntry> cache; | ||
| private final ReadWriteLock lock = new ReentrantReadWriteLock(); | ||
| private long currentSize = 0; | ||
|
|
||
| public FileCache() { | ||
| // LinkedHashMap med access-order LRU | ||
| // accessOrder=true betyder: get() och put() uppdaterar ordningen | ||
| this.cache = new LinkedHashMap<String, CacheEntry>(16, 0.75f, true); | ||
| } | ||
|
|
||
| /** | ||
| * Hämta data från cache OCH uppdatera LRU-ordningen. | ||
| * MÅSTE ha write-lock eftersom get() modifierar LinkedHashMap's internal state. | ||
| */ | ||
| public byte[] get(String key) { | ||
| Objects.requireNonNull(key, "Key kan inte vara null"); | ||
| lock.writeLock().lock(); // ← WRITE-LOCK, inte read-lock! | ||
| try { | ||
| CacheEntry entry = cache.get(key); | ||
| return entry != null ? entry.data : null; | ||
| } finally { | ||
| lock.writeLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Hämta data utan att uppdatera LRU-ordningen (för diagnostik). | ||
| * Kan använda read-lock eftersom vi inte modifierar LinkedHashMap's state. | ||
| */ | ||
| public byte[] peek(String key) { | ||
| Objects.requireNonNull(key, "Key kan inte vara null"); | ||
| lock.readLock().lock(); | ||
| try { | ||
| CacheEntry entry = cache.get(key); | ||
| return entry != null ? entry.data : null; | ||
| } finally { | ||
| lock.readLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| public void put(String key, byte[] value) { | ||
| Objects.requireNonNull(key, "Key kan inte vara null"); | ||
| Objects.requireNonNull(value, "Value kan inte vara null"); | ||
|
|
||
| lock.writeLock().lock(); | ||
| try { | ||
| // Guard mot filer större än cache | ||
| if (value.length > MAX_CACHE_SIZE) { | ||
| System.out.println("⚠️ Skipping oversized entry: " + key + | ||
| " (" + (value.length / 1024 / 1024) + "MB > " + | ||
| (MAX_CACHE_SIZE / 1024 / 1024) + "MB)"); | ||
| return; | ||
| } | ||
|
|
||
| // Ta bort gamla posten om den finns för att uppdatera storlek | ||
| CacheEntry oldEntry = cache.remove(key); | ||
| if (oldEntry != null) { | ||
| currentSize -= oldEntry.data.length; | ||
| } | ||
|
|
||
| // Evicta medan nödvändigt INNAN vi lägger till ny post | ||
| while ((currentSize + value.length > MAX_CACHE_SIZE || | ||
| cache.size() >= MAX_ENTRIES) && !cache.isEmpty()) { | ||
| evictLeastRecentlyUsedUnsafe(); | ||
| } | ||
|
|
||
| // Lägg till ny post | ||
| cache.put(key, new CacheEntry(value)); | ||
| currentSize += value.length; | ||
| } finally { | ||
| lock.writeLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Evicta minst nyligen använd entry (MÅSTE VARA UNDER WRITE LOCK) | ||
| */ | ||
| private void evictLeastRecentlyUsedUnsafe() { | ||
| // LinkedHashMap är sorterad efter access order (LRU) | ||
| // Första entry är den minst nyligen använda | ||
| Map.Entry<String, CacheEntry> eldest = cache.entrySet().iterator().next(); | ||
|
|
||
| String key = eldest.getKey(); | ||
| CacheEntry entry = eldest.getValue(); | ||
|
|
||
| cache.remove(key); | ||
| currentSize -= entry.data.length; | ||
|
|
||
| System.out.println("✗ Evicted from cache: " + key + | ||
| " (" + (entry.data.length / 1024) + "KB)"); | ||
| } | ||
|
|
||
| public void clear() { | ||
| lock.writeLock().lock(); | ||
| try { | ||
| cache.clear(); | ||
| currentSize = 0; | ||
| } finally { | ||
| lock.writeLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| public int size() { | ||
| lock.readLock().lock(); | ||
| try { | ||
| return cache.size(); | ||
| } finally { | ||
| lock.readLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| public long getCurrentSizeInBytes() { | ||
| lock.readLock().lock(); | ||
| try { | ||
| return currentSize; | ||
| } finally { | ||
| lock.readLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| public boolean contains(String key) { | ||
| lock.readLock().lock(); | ||
| try { | ||
| return cache.containsKey(key); | ||
| } finally { | ||
| lock.readLock().unlock(); | ||
| } | ||
| } | ||
|
|
||
| private static class CacheEntry { | ||
| final byte[] data; | ||
| final long timestamp; | ||
|
|
||
| CacheEntry(byte[] data) { | ||
| this.data = data; | ||
| this.timestamp = System.currentTimeMillis(); | ||
| } | ||
| } | ||
| } |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.