aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarvin Borner2019-04-22 22:07:34 +0200
committerMarvin Borner2019-04-22 22:07:34 +0200
commitc056361f5449ae6a4d94b71b0efd2f67493502f7 (patch)
treedfcbe2dc32e57048f50679c296636af9a5b59198 /src
parenta5f26def08e100bf74e90d09298ad81e9224e249 (diff)
Added viewing of files in shared directories
Co-authored-by: LarsVomMars <lars@kroenner.eu>
Diffstat (limited to 'src')
-rw-r--r--src/main/kotlin/App.kt5
-rw-r--r--src/main/kotlin/DatabaseController.kt21
-rw-r--r--src/main/kotlin/FileController.kt18
-rw-r--r--src/main/resources/js/files.js120
-rw-r--r--src/main/resources/views/files.rocker.html7
5 files changed, 112 insertions, 59 deletions
diff --git a/src/main/kotlin/App.kt b/src/main/kotlin/App.kt
index 8fb289c..39275ec 100644
--- a/src/main/kotlin/App.kt
+++ b/src/main/kotlin/App.kt
@@ -118,6 +118,11 @@ fun main() {
* Shows the shared file
*/
get("/shared", fileController::renderShared, roles(Roles.GUEST))
+
+ /**
+ * Shares file in directory
+ */
+ post("/shared", fileController::handleSharedFile, roles(Roles.GUEST))
}
}
diff --git a/src/main/kotlin/DatabaseController.kt b/src/main/kotlin/DatabaseController.kt
index b3fac60..6ec13e4 100644
--- a/src/main/kotlin/DatabaseController.kt
+++ b/src/main/kotlin/DatabaseController.kt
@@ -243,12 +243,12 @@ class DatabaseController(dbFileLocation: String = "main.db") {
}
/**
- * Returns the accessId of the given File
+ * Returns the accessId of the given file
*/
fun getAccessId(fileLocation: String, userId: Int): String {
return transaction {
try {
- FileLocation.update({ (FileLocation.userId eq userId) and (FileLocation.path eq fileLocation) }) {
+ FileLocation.update({ (FileLocation.userId eq userId) and (FileLocation.path like "$fileLocation%") }) {
it[isShared] = true
}
FileLocation.select { (FileLocation.path eq fileLocation) and (FileLocation.userId eq userId) }.map { it[FileLocation.accessId] }[0]
@@ -259,6 +259,23 @@ class DatabaseController(dbFileLocation: String = "main.db") {
}
/**
+ * Returns accessId of file in directory
+ */
+ fun getAccessIdOfDirectory(fileName: String, accessId: String): String {
+ return transaction {
+ try {
+ val fileData =
+ FileLocation.select { FileLocation.accessId eq accessId }.map { it[FileLocation.path] to it[FileLocation.userId] to it[FileLocation.isShared] }[0]
+ if (fileData.second)
+ FileLocation.select { (FileLocation.path eq "${fileData.first.first}${fileName.substring(1)}") and (FileLocation.userId eq fileData.first.second) }.map { it[FileLocation.accessId] }[0]
+ else ""
+ } catch (_: Exception) {
+ ""
+ }
+ }
+ }
+
+ /**
* Gets the shared file via [accessId]
*/
fun getSharedFile(accessId: String): ReturnFileData {
diff --git a/src/main/kotlin/FileController.kt b/src/main/kotlin/FileController.kt
index 40ca1c9..1d5bada 100644
--- a/src/main/kotlin/FileController.kt
+++ b/src/main/kotlin/FileController.kt
@@ -48,7 +48,8 @@ class FileController {
ctx.render(
"files.rocker.html", TemplateUtil.model(
"files", files,
- "path", (if (firstParam.firstOrNull() == '/') firstParam.drop(1) else firstParam)
+ "path", (if (firstParam.firstOrNull() == '/') firstParam.drop(1) else firstParam),
+ "isShared", false
)
)
}
@@ -194,12 +195,11 @@ class FileController {
)
)
} else {
- val fileProbe = Files.probeContentType(Paths.get(sharedFileLocation)) // is null if file type is not recognized
- ctx.contentType(fileProbe)
+ // TODO: Fix name of downloaded file ("shared")
+ ctx.contentType(Files.probeContentType(Paths.get(sharedFileLocation)))
ctx.result(FileInputStream(File(sharedFileLocation)))
}
} else {
- // TODO: Add support for accessing files in shared directories
// TODO: Combine the two file-crawling-render functions
val files = ArrayList<Array<String>>()
Files.list(Paths.get(sharedFileLocation)).forEach {
@@ -226,7 +226,8 @@ class FileController {
"files.rocker.html", TemplateUtil.model(
"files", files,
"path",
- (if (sharedFileData.fileLocation.firstOrNull() == '/') sharedFileData.fileLocation.drop(1) else sharedFileData.fileLocation)
+ (if (sharedFileData.fileLocation.firstOrNull() == '/') sharedFileData.fileLocation.drop(1) else sharedFileData.fileLocation),
+ "isShared", true
)
)
}
@@ -234,4 +235,11 @@ class FileController {
log.info("Unknown file!")
}
}
+
+ fun handleSharedFile(ctx: Context) {
+ val fileName = ctx.formParam("fileName").toString()
+ val accessId = ctx.formParam("accessId").toString()
+ val returnAccessId = databaseController.getAccessIdOfDirectory(fileName, accessId)
+ ctx.result(returnAccessId)
+ }
}
diff --git a/src/main/resources/js/files.js b/src/main/resources/js/files.js
index c3fa1b1..0cd15a7 100644
--- a/src/main/resources/js/files.js
+++ b/src/main/resources/js/files.js
@@ -104,68 +104,88 @@ drop.addEventListener('drop', e => {
* Set up listeners
*/
function setListeners() {
- document.querySelectorAll("[data-path]").forEach(element => {
- const images = ["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "tiff"];
- const videos = ["mp4", "m4v", "mov", "webm", "avi", "wmv", "mpg", "mpv", "mpeg", "ogv"];
- const audio = ["mp3", "m4a", "wav", "ogg"];
+ if (isShared) {
+ const accessId = location.pathname === '/shared' ? location.search.split('=')[1] : undefined;
+ document.querySelectorAll('[data-path], [data-href]').forEach(element => {
+ element.addEventListener('click', () => {
+ const request = new XMLHttpRequest();
+ const formData = new FormData();
+ formData.append('accessId', accessId);
+ formData.append('fileName', element.getAttribute('data-path') || element.getAttribute('data-href'));
+ request.open('POST', '/shared', true);
+ request.onload = () => {
+ if (request.status === 200 && request.readyState === 4) {
+ if (request.responseText)
+ window.location = `/shared?id=${request.responseText}`;
+ else alert('File not found!');
+ }
+ };
+ request.send(formData)
+ });
+ });
+ } else {
+ document.querySelectorAll("[data-path]").forEach(element => {
+ const images = ["jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "tiff"];
+ const videos = ["mp4", "m4v", "mov", "webm", "avi", "wmv", "mpg", "mpv", "mpeg", "ogv"];
+ const audio = ["mp3", "m4a", "wav", "ogg"];
- const filename = element.getAttribute("data-path");
- const extension = /(?:\.([^.]+))?$/.exec(filename)[1].toLowerCase();
+ const filename = element.getAttribute("data-path");
+ const extension = /(?:\.([^.]+))?$/.exec(filename)[1].toLowerCase();
- if (images.includes(extension)) {
- element.setAttribute("data-bp", filename);
- element.setAttribute("data-image", "");
- } else if (videos.includes(extension)) {
- element.setAttribute("data-src", filename);
- element.setAttribute("data-video", "");
- } else if (audio.includes(extension)) {
- element.setAttribute("data-src", filename);
- element.setAttribute("data-audio", "");
- }
+ if (images.includes(extension)) {
+ element.setAttribute("data-bp", filename);
+ element.setAttribute("data-image", "");
+ } else if (videos.includes(extension)) {
+ element.setAttribute("data-src", filename);
+ element.setAttribute("data-video", "");
+ } else if (audio.includes(extension)) {
+ element.setAttribute("data-src", filename);
+ element.setAttribute("data-audio", "");
+ }
- element.addEventListener("click", () => {
- if (images.indexOf(extension) === -1 && videos.indexOf(extension) === -1 && audio.indexOf(extension) === -1)
- window.location = `/files/${path}/${filename}`.clean(); // download binary files
+ element.addEventListener("click", () => {
+ if (images.indexOf(extension) === -1 && videos.indexOf(extension) === -1 && audio.indexOf(extension) === -1)
+ window.location = `/files/${path}/${filename}`.clean(); // download binary files
+ });
});
- });
- // images
- document.querySelectorAll("[data-image]").forEach(element => {
- element.addEventListener("click", image => {
- BigPicture({
- el: image.currentTarget,
- gallery: document.querySelectorAll("[data-image]")
- })
+ // images
+ document.querySelectorAll("[data-image]").forEach(element => {
+ element.addEventListener("click", image => {
+ BigPicture({
+ el: image.currentTarget,
+ gallery: document.querySelectorAll("[data-image]")
+ })
+ });
});
- });
- // videos // TODO: Fix timeout exception and scrubbing issues with chromium based browsers
- document.querySelectorAll("[data-video]").forEach(element => {
- element.addEventListener("click", video => {
- BigPicture({
- el: video.currentTarget,
- vidSrc: video.currentTarget.getAttribute("data-src")
- })
+ // videos // TODO: Fix timeout exception and scrubbing issues with chromium based browsers
+ document.querySelectorAll("[data-video]").forEach(element => {
+ element.addEventListener("click", video => {
+ BigPicture({
+ el: video.currentTarget,
+ vidSrc: video.currentTarget.getAttribute("data-src")
+ })
+ });
});
- });
- // audio // TODO: Fix IOException and scrubbing issues with chromium based browsers
- document.querySelectorAll("[data-audio]").forEach(element => {
- element.addEventListener("click", audio => {
- BigPicture({
- el: audio.currentTarget,
- audio: audio.currentTarget.getAttribute("data-src")
+ // audio // TODO: Fix IOException and scrubbing issues with chromium based browsers
+ document.querySelectorAll("[data-audio]").forEach(element => {
+ element.addEventListener("click", audio => {
+ BigPicture({
+ el: audio.currentTarget,
+ audio: audio.currentTarget.getAttribute("data-src")
+ });
});
});
- });
-
- // normal files
- document.querySelectorAll("[data-href]").forEach(element => {
- element.addEventListener("click", () => {
- window.location = `/files/${path}/${element.getAttribute("data-href")}`.clean();
- })
- });
+ // normal files
+ document.querySelectorAll("[data-href]").forEach(element => {
+ element.addEventListener("click", () => {
+ window.location = `/files/${path}/${element.getAttribute("data-href")}`.clean();
+ })
+ });
+ }
// deletion button
document.querySelectorAll(".delete").forEach(element => {
element.addEventListener("click", e => {
diff --git a/src/main/resources/views/files.rocker.html b/src/main/resources/views/files.rocker.html
index 56cfc88..eef3bb4 100644
--- a/src/main/resources/views/files.rocker.html
+++ b/src/main/resources/views/files.rocker.html
@@ -1,12 +1,15 @@
@import java.util.ArrayList
-@args (ArrayList files, String path)
+@args (ArrayList files, String path, Boolean isShared)
@css => {
<link href="/css/files.css" rel="stylesheet">
}
@js => {
-<script>const path = "@path";</script>
+<script>
+ const path = "@path";
+ const isShared = "@isShared";
+</script>
<script src="/js/imagePreview.js"></script>
<script src="/js/files.js"></script>
}