2019-04-10 20:13:38 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2016-2019 Maschell
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
2019-04-07 14:56:32 +02:00
|
|
|
package de.mas.wiiu.jnus.implementations;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2019-04-10 18:43:44 +02:00
|
|
|
import java.io.FileNotFoundException;
|
2019-04-07 14:56:32 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
import de.mas.wiiu.jnus.Settings;
|
|
|
|
import de.mas.wiiu.jnus.entities.content.Content;
|
2019-04-10 18:55:40 +02:00
|
|
|
import de.mas.wiiu.jnus.interfaces.NUSDataProvider;
|
2019-04-07 14:56:32 +02:00
|
|
|
import de.mas.wiiu.jnus.utils.StreamUtils;
|
|
|
|
import lombok.Getter;
|
|
|
|
|
2019-04-10 18:43:44 +02:00
|
|
|
public class NUSDataProviderLocalBackup implements NUSDataProvider {
|
2019-04-07 14:56:32 +02:00
|
|
|
@Getter private final String localPath;
|
|
|
|
private final short titleVersion;
|
|
|
|
|
2019-04-10 18:43:44 +02:00
|
|
|
public NUSDataProviderLocalBackup(String localPath) {
|
|
|
|
this(localPath, (short) Settings.LATEST_TMD_VERSION);
|
2019-04-07 14:56:32 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 18:43:44 +02:00
|
|
|
public NUSDataProviderLocalBackup(String localPath, short version) {
|
2019-04-07 14:56:32 +02:00
|
|
|
this.localPath = localPath;
|
|
|
|
this.titleVersion = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getFilePathOnDisk(Content c) {
|
|
|
|
return getLocalPath() + File.separator + c.getFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InputStream getInputStreamFromContent(Content content, long offset, Optional<Long> size) throws IOException {
|
|
|
|
File filepath = new File(getFilePathOnDisk(content));
|
|
|
|
if (!filepath.exists()) {
|
2019-04-10 18:43:44 +02:00
|
|
|
throw new FileNotFoundException(filepath.getAbsolutePath() + " was not found.");
|
2019-04-07 14:56:32 +02:00
|
|
|
}
|
|
|
|
InputStream in = new FileInputStream(filepath);
|
|
|
|
StreamUtils.skipExactly(in, offset);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-10 18:43:44 +02:00
|
|
|
public Optional<byte[]> getContentH3Hash(Content content) throws IOException {
|
2019-04-07 14:56:32 +02:00
|
|
|
String h3Path = getLocalPath() + File.separator + String.format("%08X.h3", content.getID());
|
|
|
|
File h3File = new File(h3Path);
|
2019-04-10 18:43:44 +02:00
|
|
|
|
|
|
|
return Optional.of(Files.readAllBytes(h3File.toPath()));
|
2019-04-07 14:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-10 18:43:44 +02:00
|
|
|
public Optional<byte[]> getRawTMD() throws IOException {
|
2019-04-07 14:56:32 +02:00
|
|
|
String inputPath = getLocalPath();
|
|
|
|
String tmdPath = inputPath + File.separator + Settings.TMD_FILENAME;
|
2019-04-10 18:57:55 +02:00
|
|
|
if (titleVersion != Settings.LATEST_TMD_VERSION) {
|
2019-04-07 14:56:32 +02:00
|
|
|
tmdPath = inputPath + File.separator + "v" + titleVersion + File.separator + Settings.TMD_FILENAME;
|
|
|
|
}
|
|
|
|
File tmdFile = new File(tmdPath);
|
2019-04-10 18:43:44 +02:00
|
|
|
return Optional.of(Files.readAllBytes(tmdFile.toPath()));
|
2019-04-07 14:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-10 18:43:44 +02:00
|
|
|
public Optional<byte[]> getRawTicket() throws IOException {
|
2019-04-07 14:56:32 +02:00
|
|
|
String inputPath = getLocalPath();
|
|
|
|
String ticketPath = inputPath + File.separator + Settings.TICKET_FILENAME;
|
|
|
|
File ticketFile = new File(ticketPath);
|
2019-04-10 18:43:44 +02:00
|
|
|
return Optional.of(Files.readAllBytes(ticketFile.toPath()));
|
2019-04-07 14:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-10 18:43:44 +02:00
|
|
|
public Optional<byte[]> getRawCert() throws IOException {
|
2019-04-07 14:56:32 +02:00
|
|
|
String inputPath = getLocalPath();
|
|
|
|
String certPath = inputPath + File.separator + Settings.CERT_FILENAME;
|
|
|
|
File certFile = new File(certPath);
|
2019-04-10 18:43:44 +02:00
|
|
|
return Optional.of(Files.readAllBytes(certFile.toPath()));
|
2019-04-07 14:56:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void cleanup() throws IOException {
|
|
|
|
// We don't need this
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
String titleVersionString = titleVersion == Settings.LATEST_TMD_VERSION ? "latest" : Short.toString(titleVersion);
|
|
|
|
return "NUSDataProviderLocalBackup [localPath=" + localPath + ", titleVersion=" + titleVersionString + "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|