@@ -3,7 +3,7 @@ defmodule FieldHubWeb.Rest.Api.Rest.File do
33
44  alias  FieldHub.FileStore 
55
6-   @ max_size   Application . compile_env ( :field_hub ,   :file_max_size ) 
6+   require   Logger 
77
88  def  index ( conn ,  % { "project"  =>  project ,  "types"  =>  types } )  when  is_list ( types )  do 
99    parsed_types  = 
@@ -89,29 +89,47 @@ defmodule FieldHubWeb.Rest.Api.Rest.File do
8989  end 
9090
9191  def  update ( conn ,  % { "project"  =>  project ,  "id"  =>  uuid ,  "type"  =>  type } )  when  is_binary ( type )  do 
92-     parsed_type  =  parse_type ( type ) 
92+     with  { :parsed_type ,  parsed_type }  when  is_atom ( parsed_type )  <- 
93+            { :parsed_type ,  parse_type ( type ) } , 
94+          { :io_opened ,  { :ok ,  io_device } }  <- 
95+            { :io_opened ,  FileStore . create_write_io_device ( uuid ,  project ,  parsed_type ) } , 
96+          { :stream ,  { :ok ,  % Plug.Conn { }  =  conn } }  <-  { :stream ,  stream_body ( conn ,  io_device ) }  do 
97+       FileStore . clear_cache ( project ) 
98+ 
99+       send_resp ( conn ,  201 ,  Jason . encode! ( % { info:  "File created." } ) ) 
100+     else 
101+       { :parsed_type ,  { :error ,  type } }  -> 
102+         send_resp ( conn ,  400 ,  Jason . encode! ( % { reason:  "Unknown file type: #{ type }  } ) ) 
103+ 
104+       { :io_opened ,  posix }  -> 
105+         Logger . error ( 
106+           "Got `#{ posix } #{ uuid } #{ type } #{ project }  
107+         ) 
108+ 
109+         send_resp ( conn ,  500 ,  Jason . encode! ( % { reason:  "Unable to write file." } ) ) 
110+ 
111+       { :stream ,  { :error ,  term } }  -> 
112+         Logger . error ( 
113+           "Got `#{ term } #{ uuid } #{ type } #{ project }  
114+         ) 
115+ 
116+         send_resp ( conn ,  500 ,  Jason . encode! ( % { reason:  "Unable to write file." } ) ) 
117+     end 
118+   end 
93119
94-      conn 
95-     |>   read_body ( length:  @ max_size ) 
120+   defp   stream_body ( conn ,   io_device )   do 
121+     read_body ( conn ) 
96122    |>  case  do 
97123      { :ok ,  data ,  conn }  -> 
98-         case  parsed_type  do 
99-           { :error ,  type }  -> 
100-             send_resp ( conn ,  400 ,  Jason . encode! ( % { reason:  "Unknown file type: #{ type }  } ) ) 
124+         IO . binwrite ( io_device ,  data ) 
125+         { :ok ,  conn } 
101126
102-           valid  -> 
103-             FileStore . store ( Zarex . sanitize ( uuid ) ,  Zarex . sanitize ( project ) ,  valid ,  data ) 
104-             send_resp ( conn ,  201 ,  Jason . encode! ( % { info:  "File created." } ) ) 
105-         end 
127+       { :more ,  data ,  conn }  -> 
128+         IO . binwrite ( io_device ,  data ) 
129+         stream_body ( conn ,  io_device ) 
106130
107-       { :more ,  _partial_body ,  conn }  -> 
108-         send_resp ( 
109-           conn , 
110-           413 , 
111-           Jason . encode! ( % { 
112-             reason:  "Payload too large, maximum of #{ Sizeable . filesize ( @ max_size ) }  
113-           } ) 
114-         ) 
131+       error  -> 
132+         error 
115133    end 
116134  end 
117135
0 commit comments