View Javadoc
1   /*
2    * Copyright (C) 2026 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.service;
7   
8   import java.time.temporal.ChronoField;
9   import java.util.UUID;
10  import org.springframework.stereotype.Service;
11  import org.tailormap.api.repository.UploadRepository;
12  
13  @Service
14  public class UploadsService {
15    private final UploadRepository uploadRepository;
16    public static final String DESCRIPTION_HEADER_NAME = "TM-Description";
17    /** The scheme used in Markdown files to reference an upload. For example, {@code upload://<upload-id>}. */
18    public static final String UPLOAD_MARKDOWN_SCHEME = "upload://";
19  
20    public UploadsService(UploadRepository uploadRepository) {
21      this.uploadRepository = uploadRepository;
22    }
23  
24    /**
25     * Checks if the upload with the given ID has been modified since the provided timestamp.
26     *
27     * @param id the UUID of the upload
28     * @param ifModifiedSince the timestamp to compare against (in milliseconds)
29     * @return true if the upload has been modified since the provided timestamp or when the upload does not exist,
30     *     false otherwise
31     */
32    public boolean checkIfModifiedSince(UUID id, long ifModifiedSince) {
33      if (ifModifiedSince == -1) {
34        return true;
35      }
36  
37      return uploadRepository
38          .findLastModifiedById(id)
39          .map(uploadLastModified -> ifModifiedSince
40              < uploadLastModified
41                  .with(ChronoField.MILLI_OF_SECOND, 0)
42                  .toInstant()
43                  .toEpochMilli())
44          .orElse(true);
45    }
46  }