-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
human-readable name together with download URLs
This enables #673, but should be backward compatible with existing UI. JSON responses containing a URL have an accompanying humanName field. This can be used as the download attribute of an HTML link, or as the attachment name in a content-disposition header. CSV download still returns text/plain (requires JSON parsing on UI side)
- Loading branch information
Showing
5 changed files
with
94 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.conveyal.file; | ||
|
||
/** | ||
* Combines a url for downloading a file, which might include a globally unique but human-annoying UUID, with a | ||
* suggested human-readable name for that file when saved by an end user. The humanName may not be globally unique, | ||
* so is only appropriate for cases where it doesn't need to be machine discoverable using a UUID. The humanName can | ||
* be used as the download attribute of an HTML link, or as the attachment name in a content-disposition header. | ||
* Instances of this record are intended to be serialized to JSON as an HTTP API response. | ||
* // TODO make this into a class with factory methods and move static cleanFilename method here. | ||
*/ | ||
public class UrlWithHumanName { | ||
public final String url; | ||
public final String humanName; | ||
|
||
public UrlWithHumanName (String url, String humanName) { | ||
this.url = url; | ||
this.humanName = humanName; | ||
} | ||
|
||
private static final int TRUNCATE_FILENAME_CHARS = 220; | ||
|
||
/** | ||
* Given an arbitrary string, make it safe for use as a friendly human-readable filename. | ||
* This can yield non-unique strings and is intended for files downloaded by end users that do not need to be | ||
* machine-discoverable by unique IDs. | ||
*/ | ||
public static String filenameCleanString (String original) { | ||
String ret = original.replaceAll("\\W+", "_"); | ||
if (ret.length() > TRUNCATE_FILENAME_CHARS) { | ||
ret = ret.substring(0, TRUNCATE_FILENAME_CHARS); | ||
} | ||
return ret; | ||
} | ||
|
||
public static UrlWithHumanName fromCleanedName (String url, String rawHumanName, String humanExtension) { | ||
String humanName = UrlWithHumanName.filenameCleanString(rawHumanName) + "." + humanExtension; | ||
return new UrlWithHumanName(url, humanName); | ||
} | ||
} |