/*
 * MIT License
 *
 * Copyright (c) 2020-present Cloudogu GmbH and Contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
    
package sonia.scm.web.lfs;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import sonia.scm.repository.Repository;
import sonia.scm.store.BlobStore;
import sonia.scm.store.BlobStoreFactory;

/**
 * Creates {@link BlobStore} objects to store lfs objects.
 * 
 * @author Sebastian Sdorra
 * @since 1.54
 */
@Singleton
public class LfsBlobStoreFactory {
  
  private static final String GIT_LFS_REPOSITORY_POSTFIX = "-git-lfs";
  
  private final BlobStoreFactory blobStoreFactory;

  /**
   * Create a new instance.
   * 
   * @param blobStoreFactory blob store factory
   */
  @Inject
  public LfsBlobStoreFactory(BlobStoreFactory blobStoreFactory) {
    this.blobStoreFactory = blobStoreFactory;
  }
  
  /**
   * Provides a {@link BlobStore} corresponding to the SCM Repository.
   * <p>
   * git-lfs repositories should generally carry the same name as their regular SCM repository counterparts. However,
   * we have decided to store them under their IDs instead of their names, since the names might change and provide
   * other drawbacks, as well.
   * <p>
   * These repositories will have {@linkplain #GIT_LFS_REPOSITORY_POSTFIX} appended to their IDs.
   *
   * @param repository The SCM Repository to provide a LFS {@link BlobStore} for.
   * 
   * @return blob store for the corresponding scm repository
   */
  @SuppressWarnings("unchecked")
  public BlobStore getLfsBlobStore(Repository repository) {
    return blobStoreFactory
        .withName(repository.getId() + GIT_LFS_REPOSITORY_POSTFIX)
        .forRepository(repository)
        .build();
  }
}
