package Ġcom . group 2 proj c . Hu ish oud . file transfer Ċ import Ġcom . group 2 proj c . Hu ish oud . database . Database Helper Ċ import Ġjava . io . IOException Ċ import Ġjava . util . stream . Collectors Ċ Ċ import Ġorg . springframework . beans . factory . annotation . Autowired Ċ import Ġorg . springframework . core . io . Resource Ċ import Ġorg . springframework . http . HttpHeaders Ċ import Ġorg . springframework . http . Response Entity Ċ import Ġorg . springframework . stereotype . Controller Ċ import Ġorg . springframework . ui . Model Ċ import Ġorg . springframework . web . bind . annotation . ExceptionHandler Ċ import Ġorg . springframework . web . bind . annotation . GetMapping Ċ import Ġorg . springframework . web . bind . annotation . PathVariable Ċ import Ġorg . springframework . web . bind . annotation . PostMapping Ċ import Ġorg . springframework . web . bind . annotation . RequestParam Ċ import Ġorg . springframework . web . bind . annotation . ResponseBody Ċ import Ġorg . springframework . web . multipart . Multipart File Ċ import Ġorg . springframework . web . servlet . mvc . method . annotation . Mvc Uri Components Builder Ċ import Ġorg . springframework . web . servlet . mvc . support . Redirect Attributes Ċ Ċ import Ġcom . group 2 proj c . Hu ish oud . file transfer . storage . Storage FileNotFoundException Ċ import Ġcom . group 2 proj c . Hu ish oud . file transfer . storage . Storage Service Ċ import Ġcom . group 2 proj c . Hu ish oud . file transfer . storage . Storage Properties Ċ import Ġorg . springframework . boot . context . properties . Enable Configuration Properties Ċ Ċ @ Controller Ċ class ĠFile Upload Controller Ġ@ Autowired Ċ constructor ( private Ġval Ġstorage Service : ĠStorage Service ) Ġ{ ĊĊĠĠĠ Ġ@ GetMapping ("/ files / read all ") ĊĠĠĠ Ġ@ Throws ( IOException :: class ) ĊĠĠĠ Ġfun Ġlist Uploaded Files ( model : ĠModel ): ĠString Ġ{ ĊĊĠĠĠĠĠĠĠ Ġmodel . addAttribute (" files ", Ġstorage Service . load All (). map ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ{ Ġpath Ġ-> ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ ĠM vc Uri Components Builder . from MethodName ( FileUpload Controller :: class . java , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" serve File ", Ġpath . get FileName (). toString ()). build (). toString () ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ}) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. collect ( Collectors . toList < Any > ())) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġ" upload Form " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ GetMapping ("/ files /{ filename :. + }") ĊĠĠĠ Ġ@ ResponseBody ĊĠĠĠ Ġfun Ġserve File (@ PathVariable Ġfilename : ĠString ): ĠResponseEntity < Resource > Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġfile Ġ= Ġstorage Service . load As Resource ( filename ) ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . ok (). header ( HttpHeaders . CONTENT _ DIS POSITION , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" attachment ; Ġfilename =\"" Ġ+ Ġfile . get Filename () Ġ+ Ġ"\"" ). body ( file ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ GetMapping ("/ files / users ") ĊĠĠĠ Ġ@ ResponseBody ĊĠĠĠ Ġfun Ġserve File ByUser (@ RequestParam (" uid ", defaultValue = Ġ" Token Not Set ") Ġuid : ĠString ): ĠResponseEntity < Resource > Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġfilename : String Ġ= ĠDatabase Helper (" jdbc : postgresql :// localhost : 5 4 3 2 / postgres "). getUser ( uid )[" picture _ link "]. toString () ĊĠĠĠĠĠĠĠ Ġval Ġfile Ġ= Ġstorage Service . load As Resource ( filename ) ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . ok (). header ( HttpHeaders . CONTENT _ DIS POSITION , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" attachment ; Ġfilename =\"" Ġ+ Ġfile . get Filename () Ġ+ Ġ"\"" ). body ( file ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ GetMapping ("/ files / tasks ") ĊĠĠĠ Ġ@ ResponseBody ĊĠĠĠ Ġfun Ġserve File By Task (@ RequestParam (" tid ", defaultValue = Ġ" Token Not Set ") Ġtid : ĠInt ): ĠResponseEntity < Resource > Ġ{ ĊĊĠĠĠĠĠĠĠ Ġval Ġfilename : String Ġ= ĠDatabase Helper (" jdbc : postgresql :// localhost : 5 4 3 2 / postgres "). get Task ( tid )[" picture link "]. toString () ĊĠĠĠĠĠĠĠ Ġval Ġfile Ġ= Ġstorage Service . load As Resource ( filename ) ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . ok (). header ( HttpHeaders . CONTENT _ DIS POSITION , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" attachment ; Ġfilename =\"" Ġ+ Ġfile . get Filename () Ġ+ Ġ"\"" ). body ( file ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ PostMapping ("/ files / upload ") ĊĠĠĠ Ġfun Ġhandle FileUpload (@ RequestParam (" file ") Ġfile : ĠMultip art File , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ RequestParam (" uid " Ġ, defaultValue Ġ= Ġ" Token Not Set ") Ġuid : ĠString , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġredirect Attributes : ĠRedirect Attributes ): ĠString Ġ{ ĊĊĠĠĠĠĠĠĠ Ġstorage Service . store ( file , uid ) ĊĠĠĠĠĠĠĠ Ġredirect Attributes . add Flash Attribute (" message ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" You Ġsuccessfully Ġuploaded Ġ" Ġ+ Ġfile . original Filename Ġ+ Ġ" !") ĊĊĠĠĠĠĠĠĠ Ġreturn Ġ" redirect :/ files / read all " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ PostMapping ("/ files / upload task ") ĊĠĠĠ Ġfun Ġhandle Task FileUpload (@ RequestParam (" file ") Ġfile : ĠMultip art File , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ@ RequestParam (" task id " Ġ, defaultValue Ġ= Ġ" Token Not Set ") Ġtask id : ĠInt , ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġredirect Attributes : ĠRedirect Attributes ): ĠString Ġ{ ĊĊĠĠĠĠĠĠĠ Ġstorage Service . store Task ( file , task id ) ĊĠĠĠĠĠĠĠ Ġredirect Attributes . add Flash Attribute (" message ", ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ" You Ġsuccessfully Ġuploaded Ġ" Ġ+ Ġfile . original Filename Ġ+ Ġ" !") ĊĊĠĠĠĠĠĠĠ Ġreturn Ġ" redirect :/ files / read all " ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ ExceptionHandler ( Storage FileNotFoundException :: class ) ĊĠĠĠ Ġfun Ġhandle Storage File NotFound ( exc : ĠStorage FileNotFoundException ): ĠResponseEntity < *> Ġ{ ĊĠĠĠĠĠĠĠ Ġreturn ĠResponseEntity . not Found (). build < Any >() ĊĠĠĠ Ġ} Ċ Ċ } Ċ