1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950includeRock.Bodyletlog_src=Logs.Src.create"opium.body.of_file"moduleLog=(valLogs.src_loglog_src:Logs.LOG)exceptionIsnt_a_fileletof_filefname=letopenLwt.Syntaxin(* TODO: allow buffer size to be configurable *)letbufsize=4096inLwt.catch(fun()->let*s=Lwt_unix.statfnameinlet*()=ifUnix.(s.st_kind<>S_REG)thenLwt.failIsnt_a_fileelseLwt.return_unitinlet*ic=Lwt_io.open_file~buffer:(Lwt_bytes.createbufsize)~flags:[O_RDONLY]~mode:Lwt_io.inputfnameinlet+size=Lwt_io.lengthicinletstream=Lwt_stream.from(fun()->Lwt.catch(fun()->let+b=Lwt_io.read~count:bufsizeicinmatchbwith|""->None|buf->Somebuf)(funexn->Log.warn(funm->m"Error while reading file %s. %s"fname(Printexc.to_stringexn));Lwt.return_none))inLwt.on_success(Lwt_stream.closedstream)(fun()->Lwt.async(fun()->Lwt_io.closeic));Some(of_stream~length:sizestream))(fune->matchewith|Isnt_a_file|Unix.Unix_error(Unix.ENOENT,_,_)->Lwt.returnNone|exn->Logs.err(funm->m"Unknown error while serving file %s. %s"fname(Printexc.to_stringexn));Lwt.failexn);;