package com.jasamedika.medifirst2000.util;

import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.stereotype.Component;

/**
 * @author salmanoe
 * @since Jan 31, 2023
 */
@Component
public class ResourceUtils {
	public static Resource loadFile(String fileName) throws Exception {
		try {
			Path filePath = Paths.get(fileName).toAbsolutePath().normalize();
			Resource resource = new UrlResource(filePath.toUri());
			if (resource.exists()) {
				return resource;
			} else {
				throw new FileNotFoundException("File not found " + fileName);
			}
		} catch (MalformedURLException e) {
			throw new FileNotFoundException("File not found " + fileName);
		}
	}
}
