Skip to content
Snippets Groups Projects
Commit d52177e2 authored by Vansh Mehta's avatar Vansh Mehta
Browse files

Updated file structure based on the new discussed directory structure.

parent a7e2bc5c
No related branches found
No related tags found
2 merge requests!31Added cut, copy and paste functionality. Disable/Enable buttons when,!23Implement directory structure
Pipeline #88657 passed
package net.codebot.backend.controller
import net.codebot.backend.service.DirectoryService
import net.codebot.backend.service.FileContentService
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping
class DirectoryController(val service: DirectoryService) {
@GetMapping("/file/content")
fun getFiles(@RequestParam path: String, @RequestParam user: String): MutableMap<String, Any?> {
@GetMapping("/directory/content")
fun getDirectory(@RequestParam path: String, @RequestParam user: String): MutableMap<String, Any?> {
return service.getDirectory(user, path)
}
@PutMapping("/file/content")
fun putFile(@RequestParam user: String, @RequestParam path: String, @RequestParam name: String): MutableMap<String, String> {
@PutMapping("/directory/content")
fun putDirectory(@RequestParam user: String, @RequestParam path: String, @RequestParam name: String): MutableMap<String, String> {
return service.createDirectory(user, path, name)
}
@PostMapping("/file/content")
fun postFile(@RequestParam user: String, @RequestParam path: String, @RequestParam oldName: String, @RequestParam newName: String): MutableMap<String, String> {
@PostMapping("/directory/content")
fun postDirectory(@RequestParam user: String, @RequestParam path: String, @RequestParam oldName: String, @RequestParam newName: String): MutableMap<String, String> {
return service.renameDirectory(user, path, oldName, newName)
}
@DeleteMapping("/file/content")
fun deleteFile(@RequestParam path: String, @RequestParam user: String): MutableMap<String, String> {
@DeleteMapping("/directory/content")
fun deleteDirectory(@RequestParam path: String, @RequestParam user: String): MutableMap<String, String> {
return service.deleteDirectory(user, path)
}
}
package net.codebot.backend.controller
import net.codebot.backend.service.FileContentService
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping
class FileContentController(val service: FileContentService) {
@GetMapping("/file/content/{user}/{id}")
fun getFiles(@PathVariable id: String, @PathVariable user: String): MutableMap<String, String> {
return service.getFileContent(user, id)
}
@PutMapping("/file/content/{user}/{id}")
fun putFile(@PathVariable user: String, @PathVariable id: String, @RequestBody content: String): MutableMap<String, String> {
return service.putFileContent(user, id, content)
}
@PostMapping("/file/content/{user}/{id}")
fun postFile(@PathVariable user: String, @PathVariable id: String, @RequestBody content: String): MutableMap<String, String> {
return service.postFileContent(user, id, content)
}
@DeleteMapping("/file/content/{user}/{id}")
fun deleteFile(@PathVariable id: String, @PathVariable user: String): MutableMap<String, String> {
return service.deleteFileContent(user, id)
}
}
//package net.codebot.backend.controller
//
//import net.codebot.backend.dto.ContentDTO
//import net.codebot.backend.service.FileService
//import org.springframework.web.bind.annotation.*
//
//@RestController
//@RequestMapping("/file")
//class FileController(val service: FileService) {
// @GetMapping("/file/{user}")
// fun getFiles(@PathVariable user: String): List<ContentDTO>? {
// return service.getFiles(user)
// }
//
// @PutMapping("/file/{user}")
// fun putFile(@RequestBody request: ContentDTO, @PathVariable user: String): MutableMap<String, String> {
// return service.createFile(request, user)
// }
//
// @PostMapping("/file/{user}")
// fun postFile(@RequestBody request: ContentDTO, @PathVariable user: String): MutableMap<String, String> {
// return service.postFile(request, user)
// }
//
// @DeleteMapping("/file/{user}")
// fun deleteFile(@RequestBody id: String, @PathVariable user: String): MutableMap<String, String> {
// return service.deleteFile(id, user)
// }
//}
package net.codebot.backend.controller
import net.codebot.backend.service.FileService
import org.springframework.web.bind.annotation.*
@RestController
@RequestMapping
class FileController(val service: FileService) {
@GetMapping("/file/content")
fun getFile(@RequestParam path: String, @RequestParam user: String): MutableMap<String, String> {
return service.getFile(user, path)
}
@PutMapping("/file/content")
fun putFile(@RequestParam user: String, @RequestParam path: String, @RequestParam name: String, @RequestBody content: String): MutableMap<String, String> {
return service.putFile(user, path, name, content)
}
@PostMapping("/file/content")
fun postFile(@RequestParam user: String, @RequestParam path: String, @RequestBody content: String): MutableMap<String, String> {
return service.postFile(user, path, content)
}
@DeleteMapping("/file/content")
fun deleteFile(@RequestParam path: String, @RequestParam user: String): MutableMap<String, String> {
return service.deleteFile(user, path)
}
@PostMapping("/file/rename")
fun renameFile(@RequestParam path: String, @RequestParam user: String, @RequestParam oldName: String, @RequestParam newName: String): MutableMap<String, String> {
return service.renameFile(user, path, oldName, newName)
}
}
//package net.codebot.backend.controller
//
//import net.codebot.backend.dto.ContentDTO
//import net.codebot.backend.service.FileService
//import org.springframework.web.bind.annotation.*
//
//@RestController
//@RequestMapping("/file")
//class FileController(val service: FileService) {
// @GetMapping("/file/{user}")
// fun getFiles(@PathVariable user: String): List<ContentDTO>? {
// return service.getFiles(user)
// }
//
// @PutMapping("/file/{user}")
// fun putFile(@RequestBody request: ContentDTO, @PathVariable user: String): MutableMap<String, String> {
// return service.createFile(request, user)
// }
//
// @PostMapping("/file/{user}")
// fun postFile(@RequestBody request: ContentDTO, @PathVariable user: String): MutableMap<String, String> {
// return service.postFile(request, user)
// }
//
// @DeleteMapping("/file/{user}")
// fun deleteFile(@RequestBody id: String, @PathVariable user: String): MutableMap<String, String> {
// return service.deleteFile(id, user)
// }
//}
......@@ -24,7 +24,7 @@ class DirectoryService {
try {
val completePath = "$basePath/$user/$path"
val directory = File(path)
val directory = File(completePath)
val contents: MutableList<ContentDTO> = mutableListOf()
if (directory.isDirectory) {
......@@ -48,6 +48,11 @@ class DirectoryService {
response["success"] = "false"
try {
val directoryPath = "$basePath/$user"
if (!File(directoryPath).isDirectory) {
Files.createDirectory(Paths.get(directoryPath))
}
Files.createDirectory(Paths.get("$basePath/$user/$path/$name"))
response["success"] = "true"
} catch (e: Exception) {
......@@ -84,13 +89,29 @@ class DirectoryService {
try {
val completePath = "$basePath/$user/$path"
File(completePath).deleteRecursively()
val directory = File(completePath)
if (directory.isDirectory) deleteDirectory(directory)
else throw Exception("path not a directory")
response["success"] = "true"
} catch (e: Exception) {
println(e.message)
response["message"] = e.message.toString()
}
return response
}
fun deleteDirectory(directory: File) {
try {
for (file in directory.listFiles()!!) {
if (file.isDirectory) deleteDirectory(file)
else Files.delete(Paths.get(file.path))
}
Files.delete(Paths.get(directory.path))
} catch (e: Exception) {
println(e.message)
}
}
}
\ No newline at end of file
package net.codebot.backend.service
import org.springframework.stereotype.Service
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Paths
@Service
class FileContentService {
private final val basePath: String = "./fileContents"
fun getFileContent(user: String, id: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val path = "$basePath/$user/$id"
val content = File(path).readText()
response["body"] = content
response["success"] = "true"
} catch (e: Exception) {
response["message"] = e.message.toString()
println(e.message)
}
return response
}
fun putFileContent(user: String, id: String, content: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val directoryPath = "$basePath/$user"
val fileDirectory = File(directoryPath)
if (!fileDirectory.isDirectory) {
Files.createDirectory(Paths.get(directoryPath))
}
val path = "$basePath/$user/$id"
Files.createFile(Paths.get(path))
File(path).writeText(content)
response["success"] = "true"
} catch (e: Exception) {
response["message"] = e.message.toString()
println(e)
}
return response
}
fun postFileContent(user: String, id: String, content: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val path = "$basePath/$user/$id"
File(path).writeText(content)
response["success"] = "true"
} catch (e: Exception) {
response["message"] = e.message.toString()
println(e)
}
return response
}
fun deleteFileContent(user: String, id: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val path = "$basePath/$user/$id"
File(path).delete()
response["success"] = "true"
} catch (e: Exception) {
println(e.message)
}
return response
}
}
\ No newline at end of file
//package net.codebot.backend.service
//
//import net.codebot.backend.dto.ContentDTO
//import org.springframework.stereotype.Service
//import java.sql.Connection
//import java.sql.PreparedStatement
//import java.sql.SQLException
//
//@Service
//class FileService(databaseSvc: DatabaseService) {
// private final var conn: Connection? = databaseSvc.connection()
//
// init {
// try {
// if (conn != null) {
// val sql = "CREATE TABLE files (id VARCHAR(255), name VARCHAR(255), path VARCHAR(255), PRIMARY KEY (id))"
// val query = conn!!.createStatement()
// query.execute(sql)
// println("Created File Table:")
// }
// } catch (ex: SQLException) {
// println(ex.message)
// }
// }
//
// fun getFiles(user: String): List<ContentDTO>? {
// var retVal = mutableListOf<ContentDTO>()
// try {
// if (conn != null) {
// val sql = "SELECT * FROM files"
// val query = conn!!.createStatement()
// val results = query.executeQuery(sql)
// println("Fetched data:");
// while (results.next()) {
// val id = results.getString("id")
// val name = results.getString("name")
// val path = results.getString("path")
//
// val file = ContentDTO(id, name, path)
// retVal.add(file)
// }
// }
// } catch (ex: SQLException) {
// println(ex.message)
// }
//
// return retVal
// }
//
// fun createFile(file: ContentDTO, user: String): MutableMap<String, String> {
// val response = mutableMapOf<String, String>()
// response["success"] = "false"
// try {
// if (conn != null) {
//// val sql = "insert into files values ('" + file.id + "', '" + file.name + "', '" + file.path + "')"
// val sql = "INSERT INTO files VALUES (?, ?, ?)"
// val preparedStatement: PreparedStatement = conn!!.prepareStatement(sql)
// preparedStatement.setString(1, file.id)
// preparedStatement.setString(2, file.name)
// preparedStatement.setString(3, file.path)
//
// val result: Int = preparedStatement.executeUpdate()
//
// if (result != 0) {
// println("Successfully added file to the db")
// response["success"] = "true"
// } else {
// println("Failed to add file to the db")
// }
// }
// } catch (ex: SQLException) {
// response["message"] = ex.message.toString()
// println(ex.message)
// }
//
// return response
// }
//
// fun postFile(file: ContentDTO, user: String): MutableMap<String, String> {
// val response = mutableMapOf<String, String>()
// response["success"] = "false"
// try {
// if (conn != null) {
//// val sql = "insert into files values ('" + file.id + "', '" + file.name + "', '" + file.path + "')"
// val sql = "UPDATE files SET name = ? AND path = ? WHERE id = ?"
// val preparedStatement: PreparedStatement = conn!!.prepareStatement(sql)
// preparedStatement.setString(3, file.id)
// preparedStatement.setString(1, file.name)
// preparedStatement.setString(2, file.path)
//
// val result: Int = preparedStatement.executeUpdate()
//
// if (result != 0) {
// println("Successfully updated file in the db")
// response["success"] = "true"
// } else {
// println("Failed to update file in the db")
// }
// }
// } catch (ex: SQLException) {
// response["message"] = ex.message.toString()
// println(ex.message)
// }
//
// return response
// }
//
// fun deleteFile(id: String, user: String): MutableMap<String, String> {
// val response = mutableMapOf<String, String>()
// response["success"] = "false"
// try {
// if (conn != null) {
// val sql = "DELETE FROM files WHERE id = ?"
// val preparedStatement: PreparedStatement = conn!!.prepareStatement(sql)
// preparedStatement.setString(1, id)
//
// val result: Int = preparedStatement.executeUpdate()
//
// if (result != 0) {
// println("Successfully delete file from the db")
// response["success"] = "true"
// } else {
// println("Failed to delete file from the db")
// }
// }
// } catch (ex: SQLException) {
// response["message"] = ex.message.toString()
// println(ex.message)
// }
//
// return response
// }
//}
\ No newline at end of file
package net.codebot.backend.service
import org.springframework.stereotype.Service
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
@Service
class FileService {
private final val basePath: String = "./fileContents"
fun getFile(user: String, path: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val completePath = "$basePath/$user/$path"
val content = File(completePath).readText()
response["body"] = content
response["success"] = "true"
} catch (e: Exception) {
response["message"] = e.message.toString()
}
return response
}
fun putFile(user: String, path: String, name: String, content: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val directoryPath = "$basePath/$user"
if (!File(directoryPath).isDirectory) {
Files.createDirectory(Paths.get(directoryPath))
}
val completePath = "$basePath/$user/$path/$name"
Files.createFile(Paths.get(completePath))
File(completePath).writeText(content)
response["success"] = "true"
} catch (e: Exception) {
response["message"] = e.message.toString()
println(e)
}
return response
}
fun postFile(user: String, path: String, content: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val completePath = "$basePath/$user/$path"
File(completePath).writeText(content)
response["success"] = "true"
} catch (e: Exception) {
response["message"] = e.message.toString()
println(e)
}
return response
}
fun deleteFile(user: String, path: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val completePath = "$basePath/$user/$path"
File(completePath).delete()
response["success"] = "true"
} catch (e: Exception) {
println(e.message)
}
return response
}
fun renameFile(user: String, path: String, oldName: String, newName: String): MutableMap<String, String> {
val response = mutableMapOf<String, String>()
response["success"] = "false"
try {
val oldPath = "$basePath/$user/$path/$oldName"
val newPath = "$basePath/$user/$path/$newName"
File(oldPath).renameTo(File(newPath))
response["success"] = "true"
} catch (e: Exception) {
println(e.message)
}
return response
}
}
\ No newline at end of file
//package net.codebot.backend.service
//
//import net.codebot.backend.dto.ContentDTO
//import org.springframework.stereotype.Service
//import java.sql.Connection
//import java.sql.PreparedStatement
//import java.sql.SQLException
//
//@Service
//class FileService(databaseSvc: DatabaseService) {
// private final var conn: Connection? = databaseSvc.connection()
//
// init {
// try {
// if (conn != null) {
// val sql = "CREATE TABLE files (id VARCHAR(255), name VARCHAR(255), path VARCHAR(255), PRIMARY KEY (id))"
// val query = conn!!.createStatement()
// query.execute(sql)
// println("Created File Table:")
// }
// } catch (ex: SQLException) {
// println(ex.message)
// }
// }
//
// fun getFiles(user: String): List<ContentDTO>? {
// var retVal = mutableListOf<ContentDTO>()
// try {
// if (conn != null) {
// val sql = "SELECT * FROM files"
// val query = conn!!.createStatement()
// val results = query.executeQuery(sql)
// println("Fetched data:");
// while (results.next()) {
// val id = results.getString("id")
// val name = results.getString("name")
// val path = results.getString("path")
//
// val file = ContentDTO(id, name, path)
// retVal.add(file)
// }
// }
// } catch (ex: SQLException) {
// println(ex.message)
// }
//
// return retVal
// }
//
// fun createFile(file: ContentDTO, user: String): MutableMap<String, String> {
// val response = mutableMapOf<String, String>()
// response["success"] = "false"
// try {
// if (conn != null) {
//// val sql = "insert into files values ('" + file.id + "', '" + file.name + "', '" + file.path + "')"
// val sql = "INSERT INTO files VALUES (?, ?, ?)"
// val preparedStatement: PreparedStatement = conn!!.prepareStatement(sql)
// preparedStatement.setString(1, file.id)
// preparedStatement.setString(2, file.name)
// preparedStatement.setString(3, file.path)
//
// val result: Int = preparedStatement.executeUpdate()
//
// if (result != 0) {
// println("Successfully added file to the db")
// response["success"] = "true"
// } else {
// println("Failed to add file to the db")
// }
// }
// } catch (ex: SQLException) {
// response["message"] = ex.message.toString()
// println(ex.message)
// }
//
// return response
// }
//
// fun postFile(file: ContentDTO, user: String): MutableMap<String, String> {
// val response = mutableMapOf<String, String>()
// response["success"] = "false"
// try {
// if (conn != null) {
//// val sql = "insert into files values ('" + file.id + "', '" + file.name + "', '" + file.path + "')"
// val sql = "UPDATE files SET name = ? AND path = ? WHERE id = ?"
// val preparedStatement: PreparedStatement = conn!!.prepareStatement(sql)
// preparedStatement.setString(3, file.id)
// preparedStatement.setString(1, file.name)
// preparedStatement.setString(2, file.path)
//
// val result: Int = preparedStatement.executeUpdate()
//
// if (result != 0) {
// println("Successfully updated file in the db")
// response["success"] = "true"
// } else {
// println("Failed to update file in the db")
// }
// }
// } catch (ex: SQLException) {
// response["message"] = ex.message.toString()
// println(ex.message)
// }
//
// return response
// }
//
// fun deleteFile(id: String, user: String): MutableMap<String, String> {
// val response = mutableMapOf<String, String>()
// response["success"] = "false"
// try {
// if (conn != null) {
// val sql = "DELETE FROM files WHERE id = ?"
// val preparedStatement: PreparedStatement = conn!!.prepareStatement(sql)
// preparedStatement.setString(1, id)
//
// val result: Int = preparedStatement.executeUpdate()
//
// if (result != 0) {
// println("Successfully delete file from the db")
// response["success"] = "true"
// } else {
// println("Failed to delete file from the db")
// }
// }
// } catch (ex: SQLException) {
// response["message"] = ex.message.toString()
// println(ex.message)
// }
//
// return response
// }
//}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment