package com.yaoyicloud.ai.pipeline.fc;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;

import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.PojoRequestHandler;

public class ReadOssTestDataHandler implements PojoRequestHandler<ReadOssTestDataRequest, OssTestDataSet> {

	@Override
	public OssTestDataSet handleRequest(ReadOssTestDataRequest request, Context context) {
		try {
			/*
			 * use OSS mount config of fc3 to access oss files with pseudo local path. Saving the effort to maintain OSS ak/sk
			 */
			InputStream inputStream = getClass().getClassLoader().getResourceAsStream("mountPath.txt");
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			String line = reader.readLine();
			if (line == null || line.isBlank()) {
				throw new RuntimeException("mountPath file not found");
			}
			System.out.println("Listing the files with prefix " + line);
			return new OssTestDataSet(
				Files.find(Paths.get(line, request.getOssPrefix()),Integer.MAX_VALUE, (p, attr) -> attr.isRegularFile())
					.map(p -> p.toString()).collect(Collectors.toList()));
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

}
