github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/blobserver/handlers/remove.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package handlers
    18  
    19  import (
    20  	"fmt"
    21  	"log"
    22  	"net/http"
    23  
    24  	"camlistore.org/pkg/blob"
    25  	"camlistore.org/pkg/blobserver"
    26  	"camlistore.org/pkg/httputil"
    27  )
    28  
    29  const maxRemovesPerRequest = 1000
    30  
    31  func CreateRemoveHandler(storage blobserver.Storage) http.Handler {
    32  	return http.HandlerFunc(func(conn http.ResponseWriter, req *http.Request) {
    33  		handleRemove(conn, req, storage)
    34  	})
    35  }
    36  
    37  func handleRemove(conn http.ResponseWriter, req *http.Request, storage blobserver.Storage) {
    38  	if req.Method != "POST" {
    39  		log.Fatalf("Invalid method; handlers misconfigured")
    40  	}
    41  
    42  	configer, ok := storage.(blobserver.Configer)
    43  	if !ok {
    44  		conn.WriteHeader(http.StatusForbidden)
    45  		fmt.Fprintf(conn, "Remove handler's blobserver.Storage isn't a blobserver.Configer; can't remove")
    46  		return
    47  	}
    48  	if !configer.Config().Deletable {
    49  		conn.WriteHeader(http.StatusForbidden)
    50  		fmt.Fprintf(conn, "storage does not permit deletes.\n")
    51  		return
    52  	}
    53  
    54  	n := 0
    55  	toRemove := make([]blob.Ref, 0)
    56  	toRemoveStr := make([]string, 0)
    57  	for {
    58  		n++
    59  		if n > maxRemovesPerRequest {
    60  			httputil.BadRequestError(conn,
    61  				fmt.Sprintf("Too many removes in this request; max is %d", maxRemovesPerRequest))
    62  			return
    63  		}
    64  		key := fmt.Sprintf("blob%v", n)
    65  		value := req.FormValue(key)
    66  		if value == "" {
    67  			break
    68  		}
    69  		ref, ok := blob.Parse(value)
    70  		if !ok {
    71  			httputil.BadRequestError(conn, "Bogus blobref for key "+key)
    72  			return
    73  		}
    74  		toRemove = append(toRemove, ref)
    75  		toRemoveStr = append(toRemoveStr, ref.String())
    76  	}
    77  
    78  	err := storage.RemoveBlobs(toRemove)
    79  	if err != nil {
    80  		conn.WriteHeader(http.StatusInternalServerError)
    81  		log.Printf("Server error during remove: %v", err)
    82  		fmt.Fprintf(conn, "Server error")
    83  		return
    84  	}
    85  
    86  	reply := make(map[string]interface{}, 0)
    87  	reply["removed"] = toRemoveStr
    88  	httputil.ReturnJSON(conn, reply)
    89  }