package com.xunmei.common.core.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 文件前缀
 */
@Component
public class FileListDeserializer extends JsonDeserializer<List<Map<String, Object>>> {

    @Override
    public List<Map<String, Object>> deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException {
        if (p.getCurrentToken() == JsonToken.START_ARRAY) {
            List<Map<String, Object>> resourceList = (List<Map<String, Object>>) p.readValueAs(List.class);
            resourceList = resourceList.stream().peek(r -> {
                String url = r.get("url").toString();
                //url = UploadProperties.INSTANCE.pureResource(url);
                r.put("url", url);
            }).collect(Collectors.toList());
            return resourceList;
        }
        return Collections.emptyList();
    }
}
