Skip to content

Commit f3548f4

Browse files
committed
on undeletion, mark image as archived so it cannot be immediately rereaped
1 parent 31c745b commit f3548f4

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

common-lib/src/main/scala/com/gu/mediaservice/GridClient.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.gu.mediaservice.model.leases.LeasesByMedia
88
import com.gu.mediaservice.model.usage.Usage
99
import com.typesafe.scalalogging.LazyLogging
1010
import play.api.http.HeaderNames
11-
import play.api.libs.json.{JsArray, JsObject, JsValue, Json, Reads}
11+
import play.api.libs.json.{JsArray, JsObject, JsTrue, JsValue, Json, Reads}
1212

1313
import scala.concurrent.duration.{Duration, DurationInt}
1414
import scala.concurrent.{ExecutionContext, Future}
@@ -260,6 +260,11 @@ class GridClient(services: Services)(implicit wsClient: WSClient) extends LazyLo
260260
authorisedRequest.post(Json.obj("data" -> data)).map { response => validateResponse(response, url)}
261261
}
262262

263+
def putArchived(mediaId: String, authFn: WSRequest => WSRequest)(implicit ec: ExecutionContext) = {
264+
val url = new URL(s"${services.metadataBaseUri}/metadata/$mediaId/archived")
265+
val request = authFn(wsClient.url(url.toString))
266+
request.put(Json.obj("data" -> JsTrue)).map { response => validateResponse(response, url)}
267+
}
263268
}
264269

265270
class DownstreamApiInBadStateException(message: String, downstreamMessage: String) extends IllegalStateException(message) {

kahuna/public/js/components/gr-undelete-image/gr-un-delete-image.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ undeleteImage.controller('grUnDeleteImageCtrl', [
2323
apiPoll(findImage);
2424
}
2525

26+
// TODO remove "unusable" banner and recognise that the image is now archived
2627
ctrl.unDeleteImage = function (image) {
2728
return mediaApi.undelete(image.data.id)
2829
.then(() => pollDeleted(image))

media-api/app/controllers/MediaApi.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,19 @@ class MediaApi(
403403
&& ImageExtras.userMayUndeleteImage(request.user, image, authorisation) =>
404404
logger.info(logMarker, s"undeleting image $id")
405405

406-
softDeletedMetadataTable.updateStatus(id, isDeleted = false)
407-
.map { _ =>
408-
messageSender.publish(
409-
UpdateMessage(
410-
subject = UnSoftDeleteImage,
411-
id = Some(id)
412-
)
406+
for {
407+
// Take an "undeletion" as a marker that the file should be kept and not reaped.
408+
// Mark as archived before undeleting, to make sure the reaper doesn't immediately reap it.
409+
_ <- gridClient.putArchived(id, auth.getOnBehalfOfPrincipal(request.user))
410+
_ <- softDeletedMetadataTable.updateStatus(id, isDeleted = false)
411+
_ = messageSender.publish(
412+
UpdateMessage(
413+
subject = UnSoftDeleteImage,
414+
id = Some(id)
413415
)
414-
}.map { _ => Accepted }
416+
)
417+
} yield Accepted
418+
415419
case Some(image) if isVisibleToAccessor(request.user, image) =>
416420
logger.info(logMarker, s"user ${request.user.accessor.identity} was not permitted to undelete image $id")
417421
Future.successful(ImageDeleteForbidden)

0 commit comments

Comments
 (0)