Skip to content

Upload track as ZIP instead of GPX to reduce file size (#64) #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@
import net.openid.appauth.TokenResponse;
import net.osmtracker.OSMTracker;
import net.osmtracker.R;
import net.osmtracker.db.DataHelper;
import net.osmtracker.db.TrackContentProvider;
import net.osmtracker.db.model.Track;
import net.osmtracker.gpx.ExportToTempFileTask;
import net.osmtracker.gpx.ZipHelper;
import net.osmtracker.osm.OpenStreetMapConstants;
import net.osmtracker.osm.UploadToOpenStreetMapTask;

import java.io.File;

/**
* <p>Uploads a track on OSM using the API and
* OAuth authentication.</p>
Expand Down Expand Up @@ -205,8 +209,10 @@ public void uploadToOsm(String accessToken) {
new ExportToTempFileTask(this, trackId) {
@Override
protected void executionCompleted() {
File fileZip = ZipHelper.zipGPXFile(context,trackId, getTmpFile());
String filename = getFilename().substring(0, getFilename().length() - 4) + DataHelper.EXTENSION_ZIP;
new UploadToOpenStreetMapTask(OpenStreetMapUpload.this, accessToken,
trackId, this.getTmpFile(), this.getFilename(),
trackId, fileZip, filename,
etDescription.getText().toString(), etTags.getText().toString(),
Track.OSMVisibility.fromPosition(
OpenStreetMapUpload.this.spVisibility.getSelectedItemPosition())
Expand Down
27 changes: 26 additions & 1 deletion app/src/main/java/net/osmtracker/gpx/ZipHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static File zipCacheFiles(Context context, long trackId, File fileGPX) {

String name = fileGPX.getName();
File zipFile = new File(context.getCacheDir(),
name.substring(0, name.length() - 3) + DataHelper.EXTENSION_ZIP);
name.substring(0, name.length() - 4) + DataHelper.EXTENSION_ZIP);

File traceFilesDirectory = DataHelper.getTrackDirectory(trackId, context);

Expand Down Expand Up @@ -64,7 +64,32 @@ public static File zipCacheFiles(Context context, long trackId, File fileGPX) {
return null;
}
}
/**
* Compresses track into a ZIP file.
*
* @param context Application context.
* @param trackId Track ID.
* @param fileGPX GPX file.
* @return The created ZIP file or null if an error occurred.
*/
public static File zipGPXFile(Context context, long trackId, File fileGPX) {

String name = fileGPX.getName();
File zipFile = new File(context.getCacheDir(),
name.substring(0, name.length() - 4) + DataHelper.EXTENSION_ZIP);

try (FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos)) {

// Add gpx file
addFileToZip(fileGPX, zos);
}
catch (IOException e) {
Log.e(TAG, "Error creating ZIP file", e);
return null;
}
return zipFile;
}

/**
* Adds a file to the ZIP archive.
Expand Down