Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
MBTilesFileConnector.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License")
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.geolocation;
20 
21 import java.nio.file.Path;
22 import java.nio.file.Paths;
23 import java.sql.Connection;
24 import java.sql.DriverManager;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.sql.Statement;
28 import org.jxmapviewer.OSMTileFactoryInfo;
29 import org.jxmapviewer.viewer.TileFactoryInfo;
31 
35 final class MBTilesFileConnector {
36 
37  private final static String DB_URL = "jdbc:sqlite:%s";
38  private final static String TILE_QUERY = "SELECT tile_data FROM images WHERE tile_id = '%s'";
39  private final static String FORMAT_QUERY = "SELECT value FROM metadata WHERE name='format'";
40  private final TileFactoryInfo factoryInfo;
41  private final String connectionString;
42 
52  static boolean isValidMBTileRasterFile(String filePath) throws SQLException {
53  Path p = Paths.get(filePath);
54  if (!p.toFile().exists()) {
55  return false;
56  }
57 
58  String path = filePath.replaceAll("\\\\", "/");
59  String url = String.format(DB_URL, path);
60 
61  try (Connection connection = DriverManager.getConnection(url)) {
62  try (Statement statement = connection.createStatement();
63  ResultSet resultSet = statement.executeQuery(FORMAT_QUERY)) {
64  if (resultSet.next()) {
65  String format = resultSet.getString(1);
66  return format.equals("jpg");
67  }
68  }
69  }
70  return false;
71  }
72 
80  MBTilesFileConnector(String tileFilePath) throws GeoLocationDataException {
81  String path = tileFilePath.replaceAll("\\\\", "/");
82  connectionString = String.format(DB_URL, path);
83  factoryInfo = new MBTilesInfo();
84  }
85 
91  TileFactoryInfo getInfo() {
92  return factoryInfo;
93  }
94 
105  byte[] getTileBytes(String tileID) throws GeoLocationDataException {
106  String query = String.format(TILE_QUERY, tileID);
107 
108  try (Connection connection = DriverManager.getConnection(connectionString)) {
109  try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query)) {
110  if (resultSet.next()) {
111  return resultSet.getBytes(1);
112  }
113  }
114  } catch (SQLException ex) {
115  throw new GeoLocationDataException(String.format("Failed to get tile %s", tileID), ex);
116  }
117  return new byte[0];
118  }
119 
124  private final class MBTilesInfo extends OSMTileFactoryInfo {
125 
126  MBTilesInfo() {
127  super("MBTilesFile", "");
128  }
129 
130  @Override
131  public String getTileUrl(int x, int y, int zoom) {
132  // OSM zoom levels are reversed from how the TileFactory deals with
133  // them.
134  int osmZoom = getTotalMapZoom() - zoom;
135  return String.format("%d/%d/%d", osmZoom, x, y);
136  }
137  }
138 }

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.