Hi,

I'm building an app (MIT license) which let's you share files easily
without depending on a third party service like CloudApp or Droplr. I'm
having the following problem; while uploading the file to your server I
want to stream the file directly to S3 and disk. Why both? Because when
the request returns, the user should be able to share the returned link
immediately. This link returns a S3 download URI. I also need to have
the file on disk to do some extra work like calculating the sha1sum and
store this to db.

I came up with the following uproach. By `loop`ing through the stream,
1024 bytes at a time I though I could write those bytes to two
outputs. I couldn't use `io/copy` because this would only allow me to
use a single output.


    (defn save-to-disk [file]
      "Saves a temporary file to disk to further analyse it"
      (let [inputstream (:body file)
            pipe-in (java.io.PipedInputStream.)
            pipe-out (java.io.PipedOutputStream. pipe-in)]
        ;; when below works, wrap it in 'future
        (with-open [in (io/input-stream inputstream)
                    file-out (io/output-stream "/tmp/test.txt")]
          (loop [buf (make-array Byte/TYPE 1024)]
            (let [len (.read in buf)]
              (when (pos? len)
                (doseq [out [file-out pipe-out]]
                  (.write out buf 0 len))
                (recur buf)))))
        (assoc file :body pipe-in)))

The above does not work, it _does_ work when I only write to
`file-out`. Writing to only `pipe-out` does not work and writing to both
stops after 1024 bytes. My understanding of IO is lacking to solve the
above, would love if somebody could educate me.

Thanks in advance!
-- 
Petar Radosevic | @wunki

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to