@@ -224,7 +224,52 @@ fn file_list_to_json(file_list: Vec<(String, PathBuf)>) -> Result<Json> {
224224 Ok ( file_list_json. to_json ( ) )
225225}
226226
227+ pub fn move_to_s3 ( conn : & Connection , n : usize ) -> Result < ( ) > {
228+ let trans = try!( conn. transaction ( ) ) ;
229+ let client = s3_client ( ) . expect ( "configured s3" ) ;
230+
231+ let rows = try!( trans. query (
232+ & format ! ( "SELECT path, mime, content FROM files WHERE content != E'in-s3' LIMIT {}" , n) ,
233+ & [ ] ) ) ;
234+
235+ let mut rt = :: tokio:: runtime:: current_thread:: Runtime :: new ( ) . unwrap ( ) ;
236+ let mut futures = Vec :: new ( ) ;
237+ for row in & rows {
238+ let path: String = row. get ( 0 ) ;
239+ let mime: String = row. get ( 1 ) ;
240+ let content: Vec < u8 > = row. get ( 2 ) ;
241+ let path_1 = path. clone ( ) ;
242+ futures. push ( client. put_object ( PutObjectRequest {
243+ bucket : "rust-docs-rs" . into ( ) ,
244+ key : path. clone ( ) ,
245+ body : Some ( content. into ( ) ) ,
246+ content_type : Some ( mime) ,
247+ ..Default :: default ( )
248+ } ) . map ( move |_| {
249+ path_1
250+ } ) . map_err ( move |e| {
251+ panic ! ( "failed to upload to {}: {:?}" , path, e)
252+ } ) ) ;
253+ }
254+
255+ use :: futures:: future:: Future ;
256+ match rt. block_on ( :: futures:: future:: join_all ( futures) ) {
257+ Ok ( paths) => {
258+ let statement = trans. prepare ( "UPDATE files SET content = E'in-s3' WHERE path = $1" )
259+ . unwrap ( ) ;
260+ for path in paths {
261+ statement. execute ( & [ & path] ) . unwrap ( ) ;
262+ }
263+ }
264+ Err ( e) => {
265+ panic ! ( "results err: {:?}" , e) ;
266+ }
267+ }
268+
269+ try!( trans. commit ( ) ) ;
227270
271+ Ok ( ( ) )
272+ }
228273
229274#[ cfg( test) ]
230275mod test {
0 commit comments