@@ -160,7 +160,6 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
160160
161161 let content: Option < Vec < u8 > > = if let Some ( client) = & client {
162162 let s3_res = client. put_object ( PutObjectRequest {
163- acl : Some ( "public-read" . into ( ) ) ,
164163 bucket : "rust-docs-rs" . into ( ) ,
165164 key : bucket_path. clone ( ) ,
166165 body : Some ( content. clone ( ) . into ( ) ) ,
@@ -225,7 +224,53 @@ fn file_list_to_json(file_list: Vec<(String, PathBuf)>) -> Result<Json> {
225224 Ok ( file_list_json. to_json ( ) )
226225}
227226
227+ pub fn move_to_s3 ( conn : & Connection , n : usize ) -> Result < usize > {
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+ let count = rows. len ( ) ;
235+
236+ let mut rt = :: tokio:: runtime:: current_thread:: Runtime :: new ( ) . unwrap ( ) ;
237+ let mut futures = Vec :: new ( ) ;
238+ for row in & rows {
239+ let path: String = row. get ( 0 ) ;
240+ let mime: String = row. get ( 1 ) ;
241+ let content: Vec < u8 > = row. get ( 2 ) ;
242+ let path_1 = path. clone ( ) ;
243+ futures. push ( client. put_object ( PutObjectRequest {
244+ bucket : "rust-docs-rs" . into ( ) ,
245+ key : path. clone ( ) ,
246+ body : Some ( content. into ( ) ) ,
247+ content_type : Some ( mime) ,
248+ ..Default :: default ( )
249+ } ) . map ( move |_| {
250+ path_1
251+ } ) . map_err ( move |e| {
252+ panic ! ( "failed to upload to {}: {:?}" , path, e)
253+ } ) ) ;
254+ }
228255
256+ use :: futures:: future:: Future ;
257+ match rt. block_on ( :: futures:: future:: join_all ( futures) ) {
258+ Ok ( paths) => {
259+ let statement = trans. prepare ( "UPDATE files SET content = E'in-s3' WHERE path = $1" )
260+ . unwrap ( ) ;
261+ for path in paths {
262+ statement. execute ( & [ & path] ) . unwrap ( ) ;
263+ }
264+ }
265+ Err ( e) => {
266+ panic ! ( "results err: {:?}" , e) ;
267+ }
268+ }
269+
270+ try!( trans. commit ( ) ) ;
271+
272+ Ok ( count)
273+ }
229274
230275#[ cfg( test) ]
231276mod test {
0 commit comments