{"action":"created","issue":{"url":"https://api.github.com/repos/RuedigerMoeller/kontraktor/issues/71","repository_url":"https://api.github.com/repos/RuedigerMoeller/kontraktor","labels_url":"https://api.github.com/repos/RuedigerMoeller/kontraktor/issues/71/labels{/name}","comments_url":"https://api.github.com/repos/RuedigerMoeller/kontraktor/issues/71/comments","events_url":"https://api.github.com/repos/RuedigerMoeller/kontraktor/issues/71/events","html_url":"https://github.com/RuedigerMoeller/kontraktor/issues/71","id":318758892,"number":71,"title":"Associating a file upload with a Session","user":{"login":"davidwynter","id":3639277,"avatar_url":"https://avatars2.githubusercontent.com/u/3639277?v=4","gravatar_id":"","url":"https://api.github.com/users/davidwynter","html_url":"https://github.com/davidwynter","followers_url":"https://api.github.com/users/davidwynter/followers","following_url":"https://api.github.com/users/davidwynter/following{/other_user}","gists_url":"https://api.github.com/users/davidwynter/gists{/gist_id}","starred_url":"https://api.github.com/users/davidwynter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidwynter/subscriptions","organizations_url":"https://api.github.com/users/davidwynter/orgs","repos_url":"https://api.github.com/users/davidwynter/repos","events_url":"https://api.github.com/users/davidwynter/events{/privacy}","received_events_url":"https://api.github.com/users/davidwynter/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-04-29T23:19:13Z","updated_at":"2018-05-02T17:51:23Z","closed_at":null,"author_association":"NONE","body":"Any advice on how I could associate a file upload with a particular session? \r\n\r\nI added a file upload handler like so in the MainApp. \r\n\r\n`\r\n\r\n\tprivate void handleFileUploadRequest(HttpServerExchange exchange) {\r\n\t\tBuilder builder = FormParserFactory.builder();\r\n\r\n\t\tfinal FormDataParser formDataParser = builder.build().createParser(exchange);\r\n\t\tif (formDataParser != null) {\r\n\t\t\texchange.startBlocking();\r\n\t\t\tFormData formData;\r\n\t\t\ttry {\r\n\t\t\t\tformData = formDataParser.parseBlocking();\r\n\t\t\t\tfor (String data : formData) {\r\n\t\t\t\t\tfor (FormData.FormValue formValue : formData.get(data)) {\r\n\t\t\t\t\t\tif (formValue.isFile()) {\r\n\t\t\t\t\t\t\tFile upload = formValue.getPath().toFile();\r\n\t\t\t\t\t\t\ttry {\r\n\t\t\t\t                String fileContents = new String(Files.readAllBytes(upload.toPath()), StandardCharsets.UTF_8);\r\n\t\t\t\t                ByteBuffer buffer = ByteBuffer.allocateDirect(fileContents.length());\r\n\t\t\t\t                buffer.put(fileContents.getBytes(UTF_8));\r\n\t\t\t\t                buffer.flip();\r\n\t\t\t\t                File file = new File(\"./images/\"+formValue.getFileName());\r\n\t\t\t\t                try (FileChannel wChannel = new FileOutputStream(file, false).getChannel()) {\r\n\t\t\t\t\t                wChannel.write(buffer);\r\n\t\t\t\t                }\r\n\t\t\t\t            } catch (IOException e) {\r\n\t\t\t\t                logger.debug(\"File upload failed during file handling with - \", e.getMessage());\r\n\t\t\t\t            }\r\n\t\t\t\t        }\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t} catch (IOException e1) {\r\n                logger.debug(\"File upload failed during Http handling with - \", e1.getMessage());\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n`\r\n\r\nwith the entry point here:\r\n\r\n`\r\n\r\n    Http4K.Build(address, port)\r\n\r\n    .httpHandler(\"/upload\", exchange -> {\r\n\t\t\t\texchange.dispatch();\r\n\t\t\t\tapp.self().handleFileUploadRequest(exchange);\r\n\t\t\t})\r\n\r\n    .httpHandler( \"/images\", exchange -> {\r\n                exchange.dispatch();\r\n                app.self().handleGetImageRequest(exchange);\r\n            })\r\n\r\n    .websocket(\"/ws\", app).serType(SerializerType.JsonNoRef).sendSid(true).buildWebsocket().build();\r\n`\r\n\r\nBut what I'd like to do is associate the file upload with the session the user is logged in on. Any way of doing this?\r\n"},"comment":{"url":"https://api.github.com/repos/RuedigerMoeller/kontraktor/issues/comments/386064005","html_url":"https://github.com/RuedigerMoeller/kontraktor/issues/71#issuecomment-386064005","issue_url":"https://api.github.com/repos/RuedigerMoeller/kontraktor/issues/71","id":386064005,"user":{"login":"RuedigerMoeller","id":6097932,"avatar_url":"https://avatars3.githubusercontent.com/u/6097932?v=4","gravatar_id":"","url":"https://api.github.com/users/RuedigerMoeller","html_url":"https://github.com/RuedigerMoeller","followers_url":"https://api.github.com/users/RuedigerMoeller/followers","following_url":"https://api.github.com/users/RuedigerMoeller/following{/other_user}","gists_url":"https://api.github.com/users/RuedigerMoeller/gists{/gist_id}","starred_url":"https://api.github.com/users/RuedigerMoeller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RuedigerMoeller/subscriptions","organizations_url":"https://api.github.com/users/RuedigerMoeller/orgs","repos_url":"https://api.github.com/users/RuedigerMoeller/repos","events_url":"https://api.github.com/users/RuedigerMoeller/events{/privacy}","received_events_url":"https://api.github.com/users/RuedigerMoeller/received_events","type":"User","site_admin":false},"created_at":"2018-05-02T17:51:23Z","updated_at":"2018-05-02T17:51:23Z","author_association":"OWNER","body":"as for your code: you are actually doing sync IO, as the handleFileupload is 'private', its not made asynchronous (only public methods are \"asynchronized\"). So either make it public (bad idea as you block the app's one and only thread then) or just run the whole upload thingy inside a dedicated thread (pool). E.g. use Actor::execInThreadPool.\r\n\r\nDoing upload using a string arg worked fine for us`(up to 10MB). E.g. `public IPromise uploadImage( String imgdata )` and from javascript: `session.ask( \"uploadImage\", base64encodedimagedata ).then( (r,e) => console.log('done')`"}}
{"actor":{"display_login":"RuedigerMoeller"}}