fukamachi / caveman

Lightweight web application framework for Common Lisp.
http://8arrow.org/caveman/
775 stars 63 forks source link

File Web Server #122

Open asarch opened 3 years ago

asarch commented 3 years ago

How do you store in the server an uploaded file?

Request:
    REQUEST-METHOD: :POST
    SCRIPT-NAME: ""
    PATH-INFO: "/"
    SERVER-NAME: "localhost"
    SERVER-PORT: 5000
    SERVER-PROTOCOL: :HTTP/1.1
    REQUEST-URI: "/"
    URL-SCHEME: "http"
    REMOTE-ADDR: "127.0.0.1"
    REMOTE-PORT: 40074
    QUERY-STRING: NIL
    RAW-BODY: #<CIRCULAR-STREAMS:CIRCULAR-INPUT-STREAM {1007D51793}>
    CONTENT-LENGTH: 477464
    CONTENT-TYPE: "multipart/form-data; boundary=----WebKitFormBoundarymApi1H0VAAu5VnIH"
    CLACK.STREAMING: T
    CLACK.IO: #<CLACK.HANDLER.HUNCHENTOOT::CLIENT {10080C1553}>
    HEADERS:
        host: "localhost:5000"
        connection: "keep-alive"
        cache-control: "max-age=0"
        upgrade-insecure-requests: "1"
        origin: "http://localhost:5000"
        user-agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
        accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
        sec-fetch-site: "same-origin"
        sec-fetch-mode: "navigate"
        sec-fetch-user: "?1"
        sec-fetch-dest: "document"
        referer: "http://localhost:5000/"
        accept-encoding: "gzip, deflate, br"
        accept-language: "es-US,es-419;q=0.9,es;q=0.8"
        cookie: "lack.session=a204a1fe96a0a36d5fb59521e5511499ed40fa27"
    COOKIES: (("lack.session" . "a204a1fe96a0a36d5fb59521e5511499ed40fa27"))
    BODY-PARAMETERS: (("the-file"
                       #<FLEXI-STREAMS::VECTOR-INPUT-STREAM {10051D57A3}>
                       "139626492_3531143143657758_2613955532857309082_o.jpg"
                       "image/jpeg"))

I mean, from this stream:

#<CIRCULAR-STREAMS:CIRCULAR-INPUT-STREAM {1007D51793}>

or from this?

 #<FLEXI-STREAMS::VECTOR-INPUT-STREAM {10051D57A3}>

And how?

jackcarrozzo commented 3 years ago

Take a look at the flexi-stream docs, there are examples for streaming into files:

http://edicl.github.io/flexi-streams/

asarch commented 3 years ago

Can you please give me an example of a POST message handler with Caveman2?

asarch commented 3 years ago

Courtesy of Mr. Nilby@#lisp.FreeNode:

(defroute ("/" :method :POST) (&key _parsed)
  (let ((uploaded-file-buffer (second (car _parsed)))
    (file-name (format nil "static/~a" (third (car _parsed)))))
    (with-open-file (file-stream file-name :direction :output :if-exists :supersede :element-type '(unsigned-byte 8))
      (uiop:copy-stream-to-stream uploaded-file-buffer file-stream :element-type '(unsigned-byte 8))))
      ;;(loop :with byte
      ;;      :while (setf byte (read-byte uploaded-file-buffer nil))
      ;;      :do (write-byte byte file-stream))))
  (redirect "/"))