Skip to content

Commit

Permalink
Simplify file URL generation.
Browse files Browse the repository at this point in the history
Only reference zipcontent in special case.
  • Loading branch information
rtibbles committed Aug 1, 2021
1 parent 02a3ffa commit 352d07e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 42 deletions.
24 changes: 9 additions & 15 deletions kolibri/core/assets/src/core-app/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,21 @@ const urls = {
}
return generateUrl(this.__mediaUrl, { url });
},
storageUrl(fileId, extension, embeddedFilePath = '') {
zipContentUrl(fileId, extension, embeddedFilePath = '') {
const filename = `${fileId}.${extension}`;
if (['zip', 'h5p'].includes(extension)) {
if (!this.__zipContentUrl) {
throw new ReferenceError('Zipcontent Url is not defined');
}
return generateUrl(this.__zipContentUrl, {
url: `${filename}/${embeddedFilePath}`,
origin: this.__zipContentOrigin,
port: this.__zipContentPort,
});
}
if (!this.__contentUrl) {
if (!this.__zipContentUrl) {
throw new ReferenceError('Zipcontent Url is not defined');
}
return generateUrl(this.__contentUrl, { url: `${filename[0]}/${filename[1]}/${filename}` });
return generateUrl(this.__zipContentUrl, {
url: `${filename}/${embeddedFilePath}`,
origin: this.__zipContentOrigin,
port: this.__zipContentPort,
});
},
downloadUrl(fileId, extension) {
storageUrl(fileId, extension) {
const filename = `${fileId}.${extension}`;
if (!this.__contentUrl) {
throw new ReferenceError('Content Url is not defined');
throw new ReferenceError('Zipcontent Url is not defined');
}
return generateUrl(this.__contentUrl, { url: `${filename[0]}/${filename[1]}/${filename}` });
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

<script>
import urls from 'kolibri.urls';
import { getFilePresetString } from './filePresetStrings';
export default {
Expand All @@ -32,7 +31,7 @@
const label = getFilePresetString(file);
return {
label,
url: urls.downloadUrl(file.checksum, file.extension),
url: file.storage_url,
fileName: this.$tr('downloadFilename', {
resourceTitle: this.nodeTitle,
fileExtension: file.extension,
Expand Down
33 changes: 9 additions & 24 deletions kolibri/core/content/utils/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
# valid storage filenames consist of 32-char hex plus a file extension
VALID_STORAGE_FILENAME = re.compile(r"[0-9a-f]{32}(-data)?\.[0-9a-z]+")

# set of file extensions that should be considered zip files and allow access to internal files
POSSIBLE_ZIPPED_FILE_EXTENSIONS = set([".zip"])


def _maybe_makedirs(path):
if not os.path.isdir(path):
Expand Down Expand Up @@ -49,7 +46,15 @@ def get_local_content_storage_file_url(obj):
The same url will also be exposed by the file serializer.
"""
if get_attribute(obj, "available"):
return get_content_storage_file_url(filename=get_content_file_name(obj))
filename = get_content_file_name(obj)
return "/{}{}/{}/{}".format(
get_content_storage_url(
conf.OPTIONS["Deployment"]["URL_PATH_PREFIX"]
).lstrip("/"),
filename[0],
filename[1],
filename,
)
else:
return None

Expand Down Expand Up @@ -305,23 +310,3 @@ def zip_content_static_root():

def get_hashi_path():
return "{}{}{}".format(zip_content_static_root(), HASHI, get_hashi_html_filename())


def get_content_storage_file_url(filename):
"""
Return the URL at which the specified file can be accessed. For regular files, this is a link to the static
file itself, under "/content/storage/". For "zip" files, this points to a dynamically generated view that
allows the client-side to index into the files within the zip.
"""
ext = os.path.splitext(filename)[1]
if ext in POSSIBLE_ZIPPED_FILE_EXTENSIONS:
return "{}{}/".format(get_zip_content_base_path(), filename)
else:
return "/{}{}/{}/{}".format(
get_content_storage_url(
conf.OPTIONS["Deployment"]["URL_PATH_PREFIX"]
).lstrip("/"),
filename[0],
filename[1],
filename,
)
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@
this.hashi.initialize(
(this.extraFields && this.extraFields.contentState) || {},
this.userData,
this.defaultFile.storage_url,
this.defaultFile.extension === 'zip'
? urls.zipContentUrl(this.defaultFile.checksum, this.defaultFile.extension)
: this.defaultFile.storage_url,
this.defaultFile.checksum
);
this.$emit('startTracking');
Expand Down

0 comments on commit 352d07e

Please sign in to comment.