()
| 2097 | } |
| 2098 | |
| 2099 | func (h *handler) handlePurge() error { |
| 2100 | h.assertAdminOnly() |
| 2101 | |
| 2102 | message := "OK" |
| 2103 | |
| 2104 | // Get the list of docs to purge |
| 2105 | |
| 2106 | input, err := h.readJSON() |
| 2107 | if err != nil { |
| 2108 | return base.HTTPErrorf(http.StatusBadRequest, "_purge document ID's must be passed as a JSON") |
| 2109 | } |
| 2110 | |
| 2111 | startTime := time.Now() |
| 2112 | docIDs := make([]string, 0) |
| 2113 | |
| 2114 | h.setHeader("Content-Type", "application/json") |
| 2115 | h.setHeader("Cache-Control", "private, max-age=0, no-cache, no-store") |
| 2116 | _, _ = h.response.Write([]byte("{\"purged\":{\r\n")) |
| 2117 | var first bool = true |
| 2118 | |
| 2119 | for key, value := range input { |
| 2120 | // For each one validate that the revision list is set to ["*"], otherwise skip doc and log warning |
| 2121 | base.InfofCtx(h.ctx(), base.KeyCRUD, "purging document = %v", base.UD(key)) |
| 2122 | |
| 2123 | if revisionList, ok := value.([]interface{}); ok { |
| 2124 | |
| 2125 | // There should only be a single revision entry of "*" |
| 2126 | if len(revisionList) != 1 { |
| 2127 | base.InfofCtx(h.ctx(), base.KeyCRUD, "Revision list for doc ID %v, should contain exactly one entry", base.UD(key)) |
| 2128 | continue // skip this entry its not valid |
| 2129 | } |
| 2130 | |
| 2131 | if revisionList[0] != "*" { |
| 2132 | base.InfofCtx(h.ctx(), base.KeyCRUD, "Revision entry for doc ID %v, should be the '*' revison", base.UD(key)) |
| 2133 | continue // skip this entry its not valid |
| 2134 | } |
| 2135 | |
| 2136 | // Attempt to delete document, if successful add to response, otherwise log warning |
| 2137 | err = h.collection.Purge(h.ctx(), key, true) |
| 2138 | if err == nil { |
| 2139 | |
| 2140 | docIDs = append(docIDs, key) |
| 2141 | |
| 2142 | if first { |
| 2143 | first = false |
| 2144 | } else { |
| 2145 | _, _ = h.response.Write([]byte(",")) |
| 2146 | } |
| 2147 | |
| 2148 | s := fmt.Sprintf("\"%v\" : [\"*\"]\n", key) |
| 2149 | _, _ = h.response.Write([]byte(s)) |
| 2150 | |
| 2151 | } else { |
| 2152 | base.InfofCtx(h.ctx(), base.KeyCRUD, "Failed to purge document %v, err = %v", base.UD(key), err) |
| 2153 | continue // skip this entry its not valid |
| 2154 | } |
| 2155 | |
| 2156 | } else { |
nothing calls this directly
no test coverage detected