@@ -43,11 +43,11 @@ module.exports = new api.object_api.Server({
4343 *
4444 */
4545function create_multipart_upload ( req ) {
46- return get_bucket_from_cache ( req )
47- . then ( function ( bucket ) {
46+ return load_bucket ( req )
47+ . then ( function ( ) {
4848 var info = {
4949 system : req . system . id ,
50- bucket : bucket . id ,
50+ bucket : req . bucket . id ,
5151 key : req . rest_params . key ,
5252 size : req . rest_params . size ,
5353 content_type : req . rest_params . content_type ,
@@ -65,20 +65,17 @@ function create_multipart_upload(req) {
6565 *
6666 */
6767function complete_multipart_upload ( req ) {
68- return get_bucket_from_cache ( req )
69- . then ( function ( bucket ) {
70- var info = {
71- system : req . system . id ,
72- bucket : bucket . id ,
73- key : req . rest_params . key ,
74- } ;
75- var updates = {
76- $unset : {
77- upload_mode : 1
78- }
79- } ;
80- return db . ObjectMD . findOneAndUpdate ( info , updates ) . exec ( ) ;
81- } ) . thenResolve ( ) ;
68+ return find_object_md ( req )
69+ . then ( function ( obj ) {
70+ return db . ObjectMD
71+ . findByIdAndUpdate ( obj . id , {
72+ $unset : {
73+ upload_mode : 1
74+ }
75+ } )
76+ . exec ( ) ;
77+ } )
78+ . thenResolve ( ) ;
8279}
8380
8481
@@ -89,18 +86,7 @@ function complete_multipart_upload(req) {
8986 *
9087 */
9188function abort_multipart_upload ( req ) {
92- return get_bucket_from_cache ( req )
93- . then ( function ( bucket ) {
94- var info = {
95- system : req . system . id ,
96- bucket : bucket . id ,
97- key : req . rest_params . key ,
98- } ;
99- var updates = {
100- upload_mode : true
101- } ;
102- return db . ObjectMD . findOneAndUpdate ( info , updates ) . exec ( ) ;
103- } ) . thenResolve ( ) ;
89+ return delete_object ( req ) ;
10490}
10591
10692
@@ -111,25 +97,14 @@ function abort_multipart_upload(req) {
11197 *
11298 */
11399function allocate_object_part ( req ) {
114- var bucket ;
115- return get_bucket_from_cache ( req )
116- . then ( function ( bucket_arg ) {
117- bucket = bucket_arg ;
118- var info = {
119- system : req . system . id ,
120- bucket : bucket . id ,
121- key : req . rest_params . key ,
122- } ;
123- return db . ObjectMD . findOne ( info ) . exec ( ) ;
124- } )
125- . then ( db . check_not_deleted ( req , 'object' ) )
100+ return find_object_md ( req )
126101 . then ( function ( obj ) {
127102 if ( ! obj . upload_mode ) {
128103 // TODO handle the upload_mode state
129104 // throw new Error('object not in upload mode');
130105 }
131106 return object_mapper . allocate_object_part (
132- bucket ,
107+ req . bucket ,
133108 obj ,
134109 req . rest_params . start ,
135110 req . rest_params . end ,
@@ -140,21 +115,8 @@ function allocate_object_part(req) {
140115
141116
142117function report_bad_block ( req ) {
143- var bucket ;
144- var obj ;
145- return get_bucket_from_cache ( req )
146- . then ( function ( bucket_arg ) {
147- bucket = bucket_arg ;
148- var info = {
149- system : req . system . id ,
150- bucket : bucket . id ,
151- key : req . rest_params . key ,
152- } ;
153- return db . ObjectMD . findOne ( info ) . exec ( ) ;
154- } )
155- . then ( db . check_not_deleted ( req , 'object' ) )
156- . then ( function ( obj_arg ) {
157- obj = obj_arg ;
118+ return find_object_md ( req )
119+ . then ( function ( obj ) {
158120 return object_mapper . bad_block_in_part (
159121 obj ,
160122 req . rest_params . start ,
@@ -181,16 +143,7 @@ function report_bad_block(req) {
181143function read_object_mappings ( req ) {
182144 var obj ;
183145
184- return get_bucket_from_cache ( req )
185- . then ( function ( bucket ) {
186- var info = {
187- system : req . system . id ,
188- bucket : bucket . id ,
189- key : req . rest_params . key ,
190- } ;
191- return db . ObjectMD . findOne ( info ) . exec ( ) ;
192- } )
193- . then ( db . check_not_deleted ( req , 'object' ) )
146+ return find_object_md ( req )
194147 . then ( function ( obj_arg ) {
195148 obj = obj_arg ;
196149 return object_mapper . read_object_mappings (
@@ -214,16 +167,7 @@ function read_object_mappings(req) {
214167 *
215168 */
216169function read_object_md ( req ) {
217- return get_bucket_from_cache ( req )
218- . then ( function ( bucket ) {
219- var info = {
220- system : req . system . id ,
221- bucket : bucket . id ,
222- key : req . rest_params . key ,
223- } ;
224- return db . ObjectMD . findOne ( info ) . exec ( ) ;
225- } )
226- . then ( db . check_not_deleted ( req , 'object' ) )
170+ return find_object_md ( req )
227171 . then ( function ( obj ) {
228172 return get_object_info ( obj ) ;
229173 } ) ;
@@ -237,15 +181,10 @@ function read_object_md(req) {
237181 *
238182 */
239183function update_object_md ( req ) {
240- return get_bucket_from_cache ( req )
241- . then ( function ( bucket ) {
242- var info = {
243- system : req . system . id ,
244- bucket : bucket . id ,
245- key : req . rest_params . key ,
246- } ;
184+ return find_object_md ( req )
185+ . then ( function ( obj ) {
247186 var updates = _ . pick ( req . rest_params , 'content_type' ) ;
248- return db . ObjectMD . findOneAndUpdate ( info , updates ) . exec ( ) ;
187+ return db . ObjectMD . findByIdAndUpdate ( obj . id , updates ) . exec ( ) ;
249188 } )
250189 . then ( db . check_not_deleted ( req , 'object' ) )
251190 . thenResolve ( ) ;
@@ -259,16 +198,21 @@ function update_object_md(req) {
259198 *
260199 */
261200function delete_object ( req ) {
262- return get_bucket_from_cache ( req )
263- . then ( function ( bucket ) {
264- var info = {
265- system : req . system . id ,
266- bucket : bucket . id ,
267- key : req . rest_params . key ,
268- } ;
269- return db . ObjectMD . findOneAndRemove ( info ) . exec ( ) ;
201+ return load_bucket ( req )
202+ . then ( function ( ) {
203+ var query = _ . omit ( object_md_query ( req ) , 'deleted' ) ;
204+ return db . ObjectMD . findOne ( query ) . exec ( ) ;
205+ } )
206+ . then ( db . check_not_found ( req , 'object' ) )
207+ . then ( function ( obj ) {
208+ return db . ObjectMD . findByIdAndUpdate ( obj . id , {
209+ deleted : new Date ( )
210+ } ) . exec ( ) ;
270211 } )
271212 . then ( db . check_not_found ( req , 'object' ) )
213+ . then ( function ( obj ) {
214+ return object_mapper . delete_object_mappings ( obj ) ;
215+ } )
272216 . thenResolve ( ) ;
273217}
274218
@@ -280,12 +224,9 @@ function delete_object(req) {
280224 *
281225 */
282226function list_objects ( req ) {
283- return get_bucket_from_cache ( req )
284- . then ( function ( bucket ) {
285- var info = {
286- system : req . system . id ,
287- bucket : bucket . id ,
288- } ;
227+ return load_bucket ( req )
228+ . then ( function ( ) {
229+ var info = _ . omit ( object_md_query ( req ) , 'key' ) ;
289230 if ( req . rest_params . key ) {
290231 info . key = new RegExp ( req . rest_params . key ) ;
291232 }
@@ -321,10 +262,30 @@ function get_object_info(md) {
321262 return info ;
322263}
323264
324- function get_bucket_from_cache ( req ) {
265+ function load_bucket ( req ) {
325266 return db . BucketCache . get ( {
326267 system : req . system . id ,
327268 name : req . rest_params . bucket ,
328269 } )
329- . then ( db . check_not_deleted ( req , 'bucket' ) ) ;
270+ . then ( db . check_not_deleted ( req , 'bucket' ) )
271+ . then ( function ( bucket ) {
272+ req . bucket = bucket ;
273+ } ) ;
274+ }
275+
276+ function object_md_query ( req ) {
277+ return {
278+ system : req . system . id ,
279+ bucket : req . bucket . id ,
280+ key : req . rest_params . key ,
281+ deleted : null
282+ } ;
283+ }
284+
285+ function find_object_md ( req ) {
286+ return load_bucket ( req )
287+ . then ( function ( ) {
288+ return db . ObjectMD . findOne ( object_md_query ( req ) ) . exec ( ) ;
289+ } )
290+ . then ( db . check_not_deleted ( req , 'object' ) ) ;
330291}
0 commit comments