package cn.reghao.oss.store.rpc.media;

import cn.reghao.oss.api.constant.UploadChannel;
import cn.reghao.oss.store.db.repository.AudioRepository;
import cn.reghao.oss.store.rpc.SignService;
import cn.reghao.oss.api.dto.media.AudioInfo;
import cn.reghao.oss.api.dto.media.AudioUrl;
import cn.reghao.oss.api.iface.media.AudioFileService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author reghao
 * @date 2023-08-28 15:00:43
 */
@DubboService
@Service
public class AudioFileServiceImpl implements AudioFileService {
    private final AudioRepository audioRepository;
    private final SignService signService;

    public AudioFileServiceImpl(AudioRepository audioRepository, SignService signService) {
        this.audioRepository = audioRepository;
        this.signService = signService;
    }

    @Override
    public AudioInfo getAudioInfo(String audioFileId) {
        List<AudioInfo> list = audioRepository.getAudioInfo(audioFileId);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<AudioUrl> getAudioUrls(String audioFileId, long loginUser) {
        int channelId = UploadChannel.audio.getCode();
        List<AudioUrl> audioUrls = audioRepository.getAudioUrls(audioFileId);
        audioUrls.forEach(audioUrl -> {
            String url = audioUrl.getUrl();
            String signedUrl = signService.getSignedUrl(loginUser, url, channelId, 3600);
            audioUrl.setUrl(signedUrl);
        });
        return audioUrls;
    }
}
