DatabaseStorageServiceImpl.java

/*
 * Copyright 2022 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.filerepository.service.impl;

import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.DatabaseFile;
import org.genesys.filerepository.model.QDatabaseFile;
import org.genesys.filerepository.persistence.DatabaseFilePersistence;
import org.genesys.filerepository.service.BytesStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * Store file bytes directly in the database!
 */
@Service("databaseStorage")
@Slf4j
public class DatabaseStorageServiceImpl implements BytesStorageService {
	
	@Autowired
	private DatabaseFilePersistence databaseFilePersistence;

	/** {@inheritDoc} */
	@Override
	public void upsert(Path path, byte[] data) throws IOException {
		var databaseFile = databaseFilePersistence.findByPath(path.toString());
		if (databaseFile == null) {
			databaseFile = new DatabaseFile();
			databaseFile.setPath(path.toString());
		}
		databaseFile.setData(data);
		databaseFilePersistence.save(databaseFile);
	}

	/** {@inheritDoc} */
	@Override
	public void upsert(Path path, final File fileWithData) throws IOException {
		if (fileWithData == null || !fileWithData.exists()) {
			throw new IOException("File is null or does not exist.");
		}

		var databaseFile = databaseFilePersistence.findByPath(path.toString());
		if (databaseFile == null) {
			databaseFile = new DatabaseFile();
			databaseFile.setPath(path.toString());
		}
		var dataLength = fileWithData.length();
		if (dataLength > 1000*1000*100) {
			log.warn("{}bytes must be read into memory!", dataLength);
		}
		databaseFile.setData(Files.readAllBytes(fileWithData.toPath()));
		databaseFilePersistence.save(databaseFile);
	}

	/** {@inheritDoc} */
	@Override
	public void remove(Path path) throws IOException {
		var databaseFile = databaseFilePersistence.findByPath(path.toString());
		if (databaseFile != null) {
			databaseFilePersistence.delete(databaseFile);
		}
	}

	/** {@inheritDoc} */
	@Override
	public byte[] get(Path path) throws IOException {
		var databaseFile = databaseFilePersistence.findByPath(path.toString());
		byte[] data = new byte[]{};
		if (databaseFile != null) {
			data = databaseFile.getData();
		}
		return data;
	}

	/** {@inheritDoc} */
	@Override
	public void get(Path path, Consumer<InputStream> consumerOfStream) throws IOException {
		var databaseFile = databaseFilePersistence.findByPath(path.toString());
		if (databaseFile != null) {
			var data = databaseFile.getData();
			try (InputStream inputStream = new ByteArrayInputStream(data)) {
				consumerOfStream.accept(inputStream);
			}
		}
	}

	/** {@inheritDoc} */
	@Override
	public boolean exists(Path bytesFile) throws IOException, InvalidRepositoryPathException {
		return databaseFilePersistence.exists(QDatabaseFile.databaseFile.path.eq(bytesFile.toString()));
	}

	/** {@inheritDoc} */
	@Override
	public List<String> listFiles(Path path) throws InvalidRepositoryPathException {
		
		return databaseFilePersistence.findFilesByFolderPath(path).stream()
			.map(file -> Path.of(file).getFileName().toString())
			.collect(Collectors.toList());
	}
}