-
Notifications
You must be signed in to change notification settings - Fork 67
feat: Non MFS Files API tutorial #303
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
67 commits
Select commit
Hold shift + click to select a range
da63860
feat: first draft of first lesson
dominguesgm 07788f6
feat: draft of the second lesson done. also bumped ipfs version
dominguesgm a485251
feat: first draft of the 3rd lesson
dominguesgm 9c83fd5
chore: first draft of the 4th lesson
dominguesgm f312363
chore: first draft of the 5th lesson
dominguesgm 99c83b4
feat: first draft of 6th and last lesson
dominguesgm 603cdb1
chore: revisit lesson 2: change information on add method argument
dominguesgm 7979d0b
Er suggestions nonmfs (#304)
443d50c
chore: made several revisions from feedback. missing discussion on di…
dominguesgm 38421ac
copy edits to lesson 1
terichadbourne 2cb95f8
copy edits to lesson 2
terichadbourne 3598c68
copy edits to lesson 3
terichadbourne 721179a
fix typos
terichadbourne 270d7b2
remove old comments from Vue files
terichadbourne e952f8b
add MFS tutorial to resources page
terichadbourne 372a164
update featured courses
terichadbourne 383d196
fix typo
terichadbourne ae27adb
copy edits to lesson 4
terichadbourne 60518e3
chore: update approach to lesson 5
dominguesgm 5ac8aa5
Merge branch 'feat/tutorial-nonmfs' of github.com:ProtoSchool/protosc…
dominguesgm c844570
chore: update lesson 6 to match lesson 5
dominguesgm ca3d27e
chore: added content to lesson 5
dominguesgm 2f1c7bf
copy edits to Lesson 5k
terichadbourne 8e32c38
fix tutorial path
terichadbourne 6fb2b60
add IPFS Camp course to resources
terichadbourne 8e5c7e9
fix: fixed bug causing race conditions on fast executing user code
dominguesgm 69de401
copy edits to lesson 6
terichadbourne aae72cb
add resulting code block sample to L6
terichadbourne a82f488
chore: added lesson on cat file inside directory
dominguesgm 64297d8
Merge branch 'feat/tutorial-nonmfs' of github.com:ProtoSchool/protosc…
dominguesgm 7134742
copy edits to lesson 7
terichadbourne 0990fda
feat: improved lessons 7 and 8
dominguesgm a2f0191
feat: made some improvements to lesson 5
dominguesgm 19d7afb
lesson 7 copy edits
dominguesgm 3ee0f22
copy edit to lesson 5 success msg
terichadbourne d9863fc
lesson 8 copy edits, adding ls comparison
terichadbourne 325620b
fix merge issue
terichadbourne db2907a
update references to Regular Files API
terichadbourne e36ba80
update tutorial description
terichadbourne 8cf4920
fix: lesson 1: add format for mfs files API method calls
dominguesgm 8164af5
fix copy paste errors
terichadbourne 30dde25
mention IPLD when describing DAG API
terichadbourne b9851ff
change tutorial url
terichadbourne 4ba1e8e
chore: minor changes
dominguesgm 9c17177
Merge branch 'feat/tutorial-nonmfs' of github.com:ProtoSchool/protosc…
dominguesgm 21ea889
chore: update with most of the changes suggested by Alan
dominguesgm 4e66138
Merge branch 'code' into feat/tutorial-nonmfs
terichadbourne 845489e
Use ipfs in path in src/tutorials/0005/07.md
terichadbourne 7f83c47
Apply suggestions from code review
terichadbourne 873b6e9
add /ipfs/ to paths
terichadbourne 98f17d3
incorporate feedback from Alan & Marcin
terichadbourne d9b45ec
chore: change main solution in lesson 05
dominguesgm 90ed173
chore: updated file structure and contents
dominguesgm 75bb007
chore: update src/tutorials/0005/07-exercise.md
dominguesgm 59c83f5
chore: updated validation for lessons 3, 4, and 5. Fixed a bug in val…
dominguesgm 9a09a62
Merge branch 'feat/tutorial-nonmfs' of github.com:ProtoSchool/protosc…
dominguesgm f72672f
chore: update to lesson 6 validation
dominguesgm 898990b
chore: update feedback on lesson 7
dominguesgm cc946fa
chore: update lesson 8 validation
dominguesgm a26cdca
remove all references to 'root' directory
terichadbourne 07d5112
imrpove catch-all error msg
terichadbourne f085853
tweaks to error messages
terichadbourne a4bbd17
move javascript hints from solution to hints in exercise
terichadbourne de3e8e5
chore: update validation for lessons 5 and 7 according to comments
dominguesgm 6df2537
validation tweaks + new override
terichadbourne 721ef5a
rename variable
terichadbourne 6cd313c
minor copy edit
terichadbourne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "all": ["0004", "0001", "0002", "0003"], | ||
| "featured": ["0004", "0001", "0002", "0003"] | ||
| "all": ["0004", "0005", "0001", "0002", "0003"], | ||
| "featured": ["0004", "0001", "0002", "0003", "0005"] | ||
| } |
This file contains hidden or 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 hidden or 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,23 @@ | ||
|
|
||
| ## IPFS: The InterPlanetary File System | ||
|
|
||
| [IPFS](https://ipfs.io/) is a peer-to-peer (P2P) networking protocol used to share data on the distributed web. You can think of it as a file system with some unique characteristics that make it ideal for safe, decentralized sharing. | ||
|
|
||
| If you haven't yet done so, we encourage you to check out our [Decentralized Data Structures](https://proto.school/#/data-structures/) tutorial, to learn all about the decentralized web and how it compares to the one you're accustomed to. There you'll learn all about content addressing, cryptographic hashing, Content Identifiers (CIDs), and sharing with peers, all of which you'll need to understand to make the most of this tutorial. | ||
|
|
||
| ## The File API vs the DAG API | ||
|
|
||
| You can store multiple types of data with IPFS. If you've gone through our Decentralized Data Structures tutorial — or even [Blogging on the Decentralized Web](https://proto.school/#/blog) — you already know you can store primitives, objects and arrays in the network. | ||
|
|
||
| The DAG API is the most generic approach to write data to the IPFS node. What if you want to share a picture of a kitten? How would you upload it to the network and provide a way for your friends to see it? What about a larger file, such as a funny video? How should the file be placed in the Directed Acyclic Graph (DAG) — in a single block or split into chunks? These are optimization details that fall beyond the scope of the DAG API. Though it would be possible to use the DAG API to write files to an IPFS node, it would be a labour intensive task. | ||
|
|
||
| You can think of the File API as an abstraction layer above the DAG API. The File API prepares files to be placed in the network, and ensures that IPFS knows how to access them. The details of what this API actually does will be covered later in this tutorial. | ||
|
|
||
| ## The Regular File API vs the MFS File API | ||
|
|
||
| If you've read our [Mutable File System tutorial](https://proto.school/#/mutable-file-system), you may be thinking, "I've already learned how to use files on IPFS. How will this be any different?" | ||
|
|
||
| The Mutable File System (MFS) provides a File API designed to replicate familiar file-system operations such as `mkdir`, `ls`, `cp`, and others. However, the way that content is addressed in IPFS makes it an immutable file system. The address to a file or directory depends on its contents, so any change to a file will result in an entirely new address. The MFS File API works on a familiar looking file system with regular paths — like `/some/stuff` — in the local IPFS node, which hides the complexity of immutable content addressing. | ||
|
|
||
| Although MFS is very useful, the abstraction it provides hides some of the inner workings of IPFS. The Regular File API we will discuss here is instead a "bare bones" approach to managing files in IPFS. It trades the powerful abstractions of MFS for a scheme which helps you understand what is actually happening in the file system. | ||
|
|
This file contains hidden or 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,17 @@ | ||
| <template> | ||
| <Lesson :text="text" /> | ||
| </template> | ||
|
|
||
| <script> | ||
| import Lesson from '../../components/Lesson' | ||
| import text from './01.md' | ||
|
|
||
| export default { | ||
| components: { | ||
| Lesson | ||
| }, | ||
| data: () => { | ||
| return { text } | ||
| } | ||
| } | ||
| </script> |
This file contains hidden or 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 @@ | ||
| First, upload one or more files by dragging and dropping below or clicking to make a selection from your file explorer. Next, in the code editor, remove the comment markers (`//`) that precede `return files` within the `run` function. |
This file contains hidden or 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,12 @@ | ||
| As a matter of security, web browsers don't let us directly change files that live in your computer's file system. Therefore you'll need to upload one or more files to the browser that you can use throughout this tutorial. | ||
terichadbourne marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Within each exercise, you'll see that you can upload files from your computer either by dragging and dropping or selecting them from your file explorer. If you look closely at the `run` function in the code editor, you'll notice that it now takes an argument `files`. When you upload files from your computer, we'll make sure they're passed into the function as the `files` array. As long as you don't refresh your browser, these files will remain accessible for the next lesson in the tutorial, but you'll also have the option to upload different files to work with for each lesson. | ||
|
|
||
| Each element in this `files` array will be of type `File`. You can find more about the `File` type [here](https://developer.mozilla.org/en-US/docs/Web/API/File). Aside from the contents of the uploaded file, a `File` type also contains the following attributes: | ||
|
|
||
| * **name** (name of the uploaded file) | ||
| * **lastModified** (date the uploaded file was last modified in) | ||
| * **size** (size of the uploaded file) | ||
| * **type** (type of the uploaded file) | ||
|
|
||
| To practice, let's upload one or more files from your computer and take a look at what's been received by the browser as the `files` array. | ||
This file contains hidden or 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,56 @@ | ||
| <template> | ||
| <FileLesson | ||
| :text="text" | ||
| :code="code" | ||
| :validate="validate" | ||
| :modules="modules" | ||
| :exercise="exercise" | ||
| :solution="solution" /> | ||
| </template> | ||
|
|
||
| <script> | ||
| import FileLesson from '../../components/FileLesson.vue' | ||
| import text from './02.md' | ||
| import exercise from './02-exercise.md' | ||
| import { logFiles } from '../../utils/files' | ||
|
|
||
| const validate = async (result, ipfs) => { | ||
| if (!result || typeof result.length === 'undefined') { | ||
| return { fail: 'Looks like you forgot to return a result. Did you forget to remove the `//` before `return files`?' } | ||
| } else if (typeof result.length === 'number') { | ||
| const fileCount = result.length > 1 ? `${result.length} files` : '1 file' | ||
| return { | ||
| success: `You successfully uploaded ${fileCount}!`, | ||
| logDesc: "Check out the data below to see the data now accessible in the `files` array. Note that these files are only in the browser right now. In the next lesson we'll see how to add them to IPFS.", | ||
| log: logFiles(result) | ||
| } | ||
| } else { | ||
| return { fail: 'Something is wrong. Reset the code and see the instructions.' } | ||
| } | ||
| } | ||
|
|
||
| const code = `const run = async (files) => { | ||
| /* remove the '//' on the line below to complete this challenge */ | ||
| // return files | ||
| } | ||
| return run | ||
| ` | ||
|
|
||
| const solution = `const run = async (files) => { | ||
| return files | ||
| } | ||
|
|
||
| return run | ||
| ` | ||
|
|
||
| const modules = { cids: require('cids') } | ||
|
|
||
| export default { | ||
| components: { | ||
| FileLesson | ||
| }, | ||
| data: () => { | ||
| return { text, validate, code, modules, exercise, solution } | ||
| } | ||
| } | ||
| </script> |
This file contains hidden or 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,4 @@ | ||
| Let's try to add a file to your IPFS node! First, try to upload one or more files. | ||
|
|
||
| **Hint:** You can pass either a `File` or an array of `File`s to the `add` method. | ||
|
|
This file contains hidden or 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,41 @@ | ||
|
|
||
| ## Working with files in ProtoSchool | ||
| Here in our ProtoSchool tutorials, we create a new IPFS node for you in the browser each time you hit the "Submit" button in a lesson. Whenever you see `ipfs.someMethod()` in our lessons, `ipfs` is a variable that refers to your IPFS instance, also known as a node. The actions that you take only affect your own IPFS node, not nodes belonging to your peers. | ||
terichadbourne marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| We create this special IPFS node behind the scenes, so you can focus on the content of our lessons. To host a fully functional IPFS node of your own, though, IPFS must be installed on your machine, and a local daemon running in your terminal. If you haven't already experimented with this, visit our docs to learn how to [install IPFS](https://docs.ipfs.io/guides/guides/install/) and [initialize your node](https://docs.ipfs.io/introduction/usage/#initialize-the-repository). | ||
|
|
||
| As mentioned previously, the methods discussed in this tutorial are part of the [Files API](https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/FILES.md). Check the documentation for more specific details, such as options for each API function. | ||
|
|
||
| ## Add a file | ||
|
|
||
| First let's learn how to add a file to your IPFS node. We'll do this by executing the following expression: | ||
|
|
||
| ```javascript | ||
| await ipfs.add(data, [options], [callback]) | ||
dominguesgm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``` | ||
|
|
||
| So if we had a file object in our browser, accessible via a variable catPic, and we wanted to add it to our IPFS node, we could do it like so: | ||
|
|
||
| ```javascript | ||
| await ipfs.add(catPic) | ||
| ``` | ||
|
|
||
| You can find the full documentation for this method [here](https://github.com/ipfs/interface-js-ipfs-core/blob/master/SPEC/FILES.md#add). | ||
|
|
||
| It's important to note that whether you want to add a `File` or multiple `File`s, you can pass either a `File` or an array of `File`s as an argument to the `add` method. | ||
|
|
||
| The `add` function returns a `Promise`, so you can place an `await` before the function call to block the execution until the return value of the promise is ready to be used. | ||
|
|
||
| The result of this `Promise` is an array of objects with the following structure: | ||
| ```javascript | ||
| { | ||
| path: string, | ||
| hash: string, | ||
| size: number | ||
| } | ||
| ``` | ||
|
|
||
| This `hash` is what we call a `CID`, a generated address based on the content of the node. For a more in depth look of how `CID`s are generated and what they are, feel free to take a look at the [Decentralized data structures](https://proto.school/#/data-structures) tutorial. | ||
|
|
||
| In a future lesson, we will learn how to use this `hash`, or `CID`, to get the contents of a file. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.