Skip to content
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
1 change: 1 addition & 0 deletions config/cadet.exs.example
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ config :cadet,
],
uploader: [
assets_bucket: "<unique-identifier>-assets",
assets_prefix: "courses/",
sourcecasts_bucket: "<unique-identifier>-cadet-sourcecasts"
],
# Configuration for Sling integration (executing on remote devices)
Expand Down
1 change: 1 addition & 0 deletions config/dev.secrets.exs.example
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ config :cadet,
],
uploader: [
assets_bucket: "source-academy-assets",
assets_prefix: "courses-dev/",
sourcecasts_bucket: "env-cadet-sourcecasts"
],
# Configuration for Sling integration (executing on remote devices)
Expand Down
1 change: 1 addition & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ config :cadet,
],
uploader: [
assets_bucket: "test-sa-assets",
assets_prefix: "courses-test/",
sourcecasts_bucket: "test-cadet-sourcecasts"
],
remote_execution: [
Expand Down
12 changes: 8 additions & 4 deletions lib/cadet/assets/assets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Cadet.Assets.Assets do
else
file = upload_params.path

s3_path = "#{course_id}/#{folder_name}/#{file_name}"
s3_path = "#{prefix()}#{course_id}/#{folder_name}/#{file_name}"

file
|> Upload.stream_file()
Expand All @@ -36,7 +36,7 @@ defmodule Cadet.Assets.Assets do
case validate_folder_name(folder_name) do
:ok ->
bucket()
|> S3.list_objects(prefix: "#{course_id}/" <> folder_name <> "/")
|> S3.list_objects(prefix: "#{prefix()}#{course_id}/" <> folder_name <> "/")
|> ExAws.stream!()
|> Enum.map(fn file -> file.key end)

Expand All @@ -50,7 +50,7 @@ defmodule Cadet.Assets.Assets do
:ok <- validate_folder_name(folder_name) do
if object_exists?(course_id, folder_name, file_name) do
bucket()
|> S3.delete_object("#{course_id}/#{folder_name}/#{file_name}")
|> S3.delete_object("#{prefix()}#{course_id}/#{folder_name}/#{file_name}")
|> ExAws.request!()

:ok
Expand All @@ -63,7 +63,9 @@ defmodule Cadet.Assets.Assets do
@spec object_exists?(integer(), binary, binary) :: boolean()
def object_exists?(course_id, folder_name, file_name) do
response =
bucket() |> S3.head_object("#{course_id}/#{folder_name}/#{file_name}") |> ExAws.request()
bucket()
|> S3.head_object("#{prefix()}#{course_id}/#{folder_name}/#{file_name}")
|> ExAws.request()

case response do
{:error, _error} -> false
Expand Down Expand Up @@ -96,4 +98,6 @@ defmodule Cadet.Assets.Assets do
end

defp bucket, do: :cadet |> Application.fetch_env!(:uploader) |> Keyword.get(:assets_bucket)

defp prefix, do: :cadet |> Application.fetch_env!(:uploader) |> Keyword.get(:assets_prefix, "")
end
2 changes: 1 addition & 1 deletion test/cadet/assets/assets_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Cadet.Assets.AssetsTest do
"testFolder",
"test1.png"
) ===
"https://#{bucket()}.s3.amazonaws.com/1/testFolder/test1.png"
"https://#{bucket()}.s3.amazonaws.com/courses-test/1/testFolder/test1.png"
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ defmodule CadetWeb.AdminAssetsControllerTest do
})

assert json_response(conn, 200) ===
"https://#{bucket()}.s3.amazonaws.com/#{course_id}/testFolder/test.png"
"https://#{bucket()}.s3.amazonaws.com/courses-test/#{course_id}/testFolder/test.png"
end
end
end
Expand Down Expand Up @@ -191,7 +191,7 @@ defmodule CadetWeb.AdminAssetsControllerTest do
})

assert json_response(conn, 200) ===
"https://#{bucket()}.s3.amazonaws.com/#{course_id}/testFolder/nestedFolder/test.png"
"https://#{bucket()}.s3.amazonaws.com/courses-test/#{course_id}/testFolder/nestedFolder/test.png"
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/test2.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/test2.png"
},
"response": {
"binary": false,
Expand Down Expand Up @@ -49,7 +49,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/test2.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/test2.png"
},
"response": {
"binary": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/nestedFolder/test2.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/nestedFolder/test2.png"
},
"response": {
"binary": false,
Expand Down Expand Up @@ -49,7 +49,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/nestedFolder/test2.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/nestedFolder/test2.png"
},
"response": {
"binary": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"response": {
"binary": false,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test-sa-assets</Name><Prefix>117/testFolder/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>testFolder/</Key><LastModified>2020-07-04T18:53:11.000Z</LastModified><ETag>&quot;d41d8cd98f00b204e9800998ecf8427e&quot;</ETag><Size>0</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>testFolder/test.png</Key><LastModified>2020-07-04T19:04:50.000Z</LastModified><ETag>&quot;3104001edec38fadeb925b9dbddab198&quot;</ETag><Size>8035</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>testFolder/test2.png</Key><LastModified>2020-07-04T19:04:50.000Z</LastModified><ETag>&quot;3104001edec38fadeb925b9dbddab198&quot;</ETag><Size>8035</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>",
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test-sa-assets</Name><Prefix>courses-test/117/testFolder/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>testFolder/</Key><LastModified>2020-07-04T18:53:11.000Z</LastModified><ETag>&quot;d41d8cd98f00b204e9800998ecf8427e&quot;</ETag><Size>0</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>testFolder/test.png</Key><LastModified>2020-07-04T19:04:50.000Z</LastModified><ETag>&quot;3104001edec38fadeb925b9dbddab198&quot;</ETag><Size>8035</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>testFolder/test2.png</Key><LastModified>2020-07-04T19:04:50.000Z</LastModified><ETag>&quot;3104001edec38fadeb925b9dbddab198&quot;</ETag><Size>8035</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>",
"headers": {
"Date": "Sat, 04 Jul 2020 19:04:57 GMT",
"x-amz-bucket-region": "ap-southeast-1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/test.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/test.png"
},
"response": {
"binary": false,
Expand Down Expand Up @@ -45,7 +45,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/test.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/test.png"
},
"response": {
"binary": false,
Expand Down Expand Up @@ -75,7 +75,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/test.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/test.png"
},
"response": {
"binary": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/nestedFolder/test.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/nestedFolder/test.png"
},
"response": {
"binary": false,
Expand Down Expand Up @@ -45,7 +45,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/nestedFolder/test.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/nestedFolder/test.png"
},
"response": {
"binary": false,
Expand Down Expand Up @@ -75,7 +75,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/117/testFolder/nestedFolder/test.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/117/testFolder/nestedFolder/test.png"
},
"response": {
"binary": false,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/vcr_cassettes/aws/model_delete_asset#1.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/1/testFolder/test4.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/1/testFolder/test4.png"
},
"response": {
"binary": false,
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/vcr_cassettes/aws/model_delete_asset#2.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/1/testFolder/test.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/1/testFolder/test.png"
},
"response": {
"binary": false,
Expand Down Expand Up @@ -49,7 +49,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/1/testFolder/test.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/1/testFolder/test.png"
},
"response": {
"binary": false,
Expand All @@ -62,4 +62,4 @@
"type": "ok"
}
}
]
]
4 changes: 2 additions & 2 deletions test/fixtures/vcr_cassettes/aws/model_list_assets#1.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"response": {
"binary": false,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test-sa-assets</Name><Prefix>1/testFolder/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>1/testFolder/</Key><LastModified>2021-08-02T10:23:39.000Z</LastModified><ETag>&quot;d41d8cd98f00b204e9800998ecf8427e&quot;</ETag><Size>0</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>1/testFolder/test.png</Key><LastModified>2021-08-02T10:56:36.000Z</LastModified><ETag>&quot;33c5d4f094a7727780a3c63babae1083&quot;</ETag><Size>2568</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>1/testFolder/test2.png</Key><LastModified>2021-08-02T10:25:15.000Z</LastModified><ETag>&quot;af2ab457d8b118efa176bc12cff4895f&quot;</ETag><Size>2996</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>",
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test-sa-assets</Name><Prefix>courses-test/1/testFolder/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>1/testFolder/</Key><LastModified>2021-08-02T10:23:39.000Z</LastModified><ETag>&quot;d41d8cd98f00b204e9800998ecf8427e&quot;</ETag><Size>0</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>1/testFolder/test.png</Key><LastModified>2021-08-02T10:56:36.000Z</LastModified><ETag>&quot;33c5d4f094a7727780a3c63babae1083&quot;</ETag><Size>2568</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>1/testFolder/test2.png</Key><LastModified>2021-08-02T10:25:15.000Z</LastModified><ETag>&quot;af2ab457d8b118efa176bc12cff4895f&quot;</ETag><Size>2996</Size><Owner><ID>98bd2bd2de0c976fb511f741fea454cb1026b9e1f9ac9160fd9f51d07e765f19</ID><DisplayName>unixsp+cs1101s</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>",
"headers": {
"Date": "Mon, 02 Aug 2021 10:59:51 GMT",
"x-amz-bucket-region": "ap-southeast-1",
Expand All @@ -30,4 +30,4 @@
"type": "ok"
}
}
]
]
4 changes: 2 additions & 2 deletions test/fixtures/vcr_cassettes/aws/model_list_assets#2.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"response": {
"binary": false,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test-sa-assets</Name><Prefix>2/testFolder/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated></ListBucketResult>",
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test-sa-assets</Name><Prefix>courses-test/2/testFolder/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated></ListBucketResult>",
"headers": {
"Date": "Mon, 02 Aug 2021 10:59:49 GMT",
"x-amz-bucket-region": "ap-southeast-1",
Expand All @@ -30,4 +30,4 @@
"type": "ok"
}
}
]
]
4 changes: 2 additions & 2 deletions test/fixtures/vcr_cassettes/aws/model_upload_asset#1.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/1/testFolder/test2.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/1/testFolder/test2.png"
},
"response": {
"binary": false,
Expand All @@ -33,4 +33,4 @@
"type": "ok"
}
}
]
]
10 changes: 5 additions & 5 deletions test/fixtures/vcr_cassettes/aws/model_upload_asset#2.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/1/testFolder/test1.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/1/testFolder/test1.png"
},
"response": {
"binary": false,
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<InitiateMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Bucket>test-sa-assets</Bucket><Key>1/testFolder/test1.png</Key><UploadId>Jk9B8v0z6kdQbTsuU9S4Wu0r5oCPGDwid8meJ4h6sdUGwNyMKSetyBSCooSrl6zormlRXJDxmzcZMQJp9ylbq.aNQql.Vf048gE28hUz5_.bSzARS8fxYzPQFjgvxgkVLK4sCM.QSI3KtEc6JpYJHg--</UploadId></InitiateMultipartUploadResult>",
"body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<InitiateMultipartUploadResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Bucket>test-sa-assets</Bucket><Key>courses-test/1/testFolder/test1.png</Key><UploadId>Jk9B8v0z6kdQbTsuU9S4Wu0r5oCPGDwid8meJ4h6sdUGwNyMKSetyBSCooSrl6zormlRXJDxmzcZMQJp9ylbq.aNQql.Vf048gE28hUz5_.bSzARS8fxYzPQFjgvxgkVLK4sCM.QSI3KtEc6JpYJHg--</UploadId></InitiateMultipartUploadResult>",
"headers": {
"Date": "Mon, 02 Aug 2021 10:59:53 GMT",
"Transfer-Encoding": "chunked",
Expand All @@ -45,7 +45,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/1/testFolder/test1.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/1/testFolder/test1.png"
},
"response": {
"binary": false,
Expand Down Expand Up @@ -75,7 +75,7 @@
"recv_timeout": 660000
},
"request_body": "",
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/1/testFolder/test1.png"
"url": "https://s3.ap-southeast-1.amazonaws.com/test-sa-assets/courses-test/1/testFolder/test1.png"
},
"response": {
"binary": false,
Expand All @@ -90,4 +90,4 @@
"type": "ok"
}
}
]
]