Skip to content
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

Fix: uploads.json endpoint returns 500 internal server error for some courses#6046 #6114

Merged
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
27 changes: 18 additions & 9 deletions app/assets/javascripts/actions/uploads_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,34 @@ const fetchUploads = (courseId) => {
if (res.ok && res.status === 200) {
return res.json();
}
return Promise.reject(res);
return Promise.reject(new Error(`Failed to fetch uploads. Status: ${res.status}`));
})
.catch((error) => {
logErrorMessage(error);
return { error: 'Failed to fetch uploads', status: error.status || 500 };
});
};

export const receiveUploads = courseId => (dispatch) => {
return (
fetchUploads(courseId)
.then(resp => dispatch({
return fetchUploads(courseId)
.then((resp) => {
if (!resp) {
return dispatch({
type: API_FAIL,
data: { error: 'No response received' },
});
}
return dispatch({
type: RECEIVE_UPLOADS,
data: resp,
}))
.catch(resp => dispatch({
});
})
.catch((resp) => {
dispatch({
type: API_FAIL,
data: resp
}))
);
data: resp,
});
});
};

const fetchUploadMetadata = (uploads) => {
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/reducers/uploads.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const SORT_DESCENDING = {
export default function uploads(state = initialState, action) {
switch (action.type) {
case RECEIVE_UPLOADS: {
const dataUploads = action.data.course.uploads;
const dataUploads = action.data?.course?.uploads || [];
// Intial sorting by upload date
const sortedModel = sortByKey(dataUploads, 'uploaded_at', state.sortKey, SORT_DESCENDING.uploaded_at);

Expand Down
2 changes: 1 addition & 1 deletion app/helpers/uploads_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#= Helpers for course views
module UploadsHelper
def pretty_filename(upload)
pretty = upload.file_name
pretty = CGI.unescape(upload.file_name)
pretty['File:'] = ''
pretty
end
Expand Down
9 changes: 9 additions & 0 deletions spec/helpers/uploads_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@
result = pretty_filename(upload)
expect(result).to eq('My file.jpg')
end

it 'formats a complex filename with encoded characters nicely for display' do
upload = build(:commons_upload,
# rubocop:disable Layout/LineLength
file_name: 'File%3AA+sunflower+%F0%9F%8C%BB%F0%9F%8C%BB+in+Kaduna+Polytechnic%2CSabo+Campus.jpg')
# rubocop:enable Layout/LineLength
result = pretty_filename(upload)
expect(result).to eq('A sunflower 🌻🌻 in Kaduna Polytechnic,Sabo Campus.jpg')
end
end
end
Loading