Auto-generated transcript Welcome back ladies and gentlemen to part 3 of the DriveSinker project. Now I am recording this for the second time because the last time I recorded this my video file got corrupted. And so I have to record this a second time. You may see some new stuff over here like this subfolder over here. I don't think I had this at the end of part 2. But you can just ignore that for now because we are going to work with this in this part of the video. and yeah let me just quickly recap what we did we built this config struct over here including a config file that looks something like this and we also built this drive struct over here this new drive function the connect function and all of this is just going to give us the google drive service right the api that we can use to interact with our google drive to upload files and folders and so on and so forth right yeah so in this video we're going to finally implement the functionality to upload files and folders to Google Drive I am going to do this in the in a new file and I'll call this sync dot go because it's synchronizing between Google Drive and our local machine right not the most intelligent of naming sense but it'll do for us so let's create a function over here called upload folder this will upload the entire folder that we give it to google drive it will take the drive service which will be the drive struct that we created earlier and it'll take a folder which will be a pointer to the folder object that we have defined in the config over here now let's start implementing this and this will not return anything for now i don't need it to return anything first of all let's just add some logs over here let's just say a starting upload process for folder and print the folder name over here and add a new line as well there we go and now we can actually create this folder so to create the folder i'm going to create a variable called metadata and i'll create the file metadata over here we'll give it the name of folder.name and we'll give it the mime type this is important of application slash vnd.google dash apps dot folder so this is the exact mime type that you need to pass in when you're trying to create a folder in google drive right not a file a folder once you have this you can just say something like the drive API dot files dot create and pass in the metadata over here as a reference and then just run do over here and this will create the folder for us it will return a file and an error I can just call this R as the folder all right or not the folder because the argument over here is named folder as well let's call this the I don't know let's just call this project folder all right and of course handle the error as well let's just say log dot fatal F failed to create project folder print a folder name over here and print the error over here and add a new line as well pass in the arguments the folder name and the error there we go and i'm using fatal f because this will just exit the program right if this error occurs because if we can't create the project folder then there's no point in continuing and trying to upload files right because there's no folder to upload them to so with that done we can then start to upload all of the files in this um in this project folder right so for that we can use the file path package and you can use the walk the function over here this is going to take a path which will just be folder.path for us and then it's gonna run this function that we give it and it's going to run this function for every single file inside of this folder right so we can use this to actually upload every single file so let's do this now first of all we want to create the file metadata obviously so let's just call it F over here or let's actually just call it metadata again who cares right and let's create drive dot file the name will be from D dot name why is this package not imported there we go D dot name and then we want to pass in a parent as well this is going to be a list of strings you might be wondering why we are passing a list of strings as the parents for this file instead of just one parent like why doesn't every file only have one parent or one folder above it right this is kind of something that's unique to Google Drive and basically you can have multiple parents that hold the same file basically a file can have multiple parents multiple parent folders and you can kind of access the same file from all of those different folders all right so this is something that's unique to google drive we don't really need to worry about it we're just going to pass in one parent and that will be the project folder over here and we'll pass in the id of that folder over here so this will put this file inside of the project folder that we define up there all right and then what we can do is we can open up the file by saying f and error equals os dot open and then pass in the file path over here make sure to handle the error as well let's say log dot print ln or not log dot print ln but uh let's return the error over here because this function does expect to receive an error over here right so let's let's do that and make sure to close the file when we're done by using dooper and then you can actually create this file so let's say drive.api.files.create we're going to pass in the metadata as a reference don't forget that then we'll run the media function and here we'll pass in the file that we want to upload this accepts any io.reader object and luckily for us every single file is going to implement that interface as well so we can just pass in the file over here run do this will return the file and the error so let's handle both if there's an error let's just return it over here and actually now that i think about it let's let's log all of the errors as well just so we know what's going on over here right let's say fail to open file and there we go or actually I wanna I wanna I wanna have the file name over here as well so let's just do print F it's just so much easier to use print F print a string over here and the error over here and obviously add a new line as well and it has in the file name as well over here there we go let's do something similar down here and say fail to upload file and then just print the file name and the the error that we get from google drive all right awesome now that should be all we need to do let's um run a final or print a final log over here let's say upload it the file name to drive and And then let's also print the ID of that file over here. So let's pass in. And by the way, make sure to add a new line. Don't ever forget that. Print the file name over here. That'll be d.name. And the ID of the file on drive, that'll be this r variable, .id. And that's all we need to do. And we can return nil at the end because this function expects to receive an error over here, right? So we're just going to return nil if nothing breaks, right? If there's no error, that should be all we need to do. So let's go back to main.go and up here and just see if this works, right? So let's think about how we want to upload all of our folders. So we can go over to config and you can see that the config holds a list of folders and each of these folders we want to synchronize or upload to Google Drive. So we can actually loop over this. We can say something like for underscore f range. Where's the config conf dot folders? And we can say something like upload folder, pass in the drive and pass in the folder as well. And as you can see, we only have one folder to upload, which is this current project folder that I'm in. So let's just see if this works right. and this will probably first um make me refresh the authorization token because i haven't opened this project in a while but let's see let's see what happens okay so looks like my authorization token already works but apparently you can't synchronize a folder so let me go back over here basically what's going on is what's going on is it's trying to upload this whole folder first and we can't do that so let's what we should do over here is let's say if d dot is dir then we can just say return nil over here so we're skipping uploading directories for now we just want to upload files all right so let's clear this let's run the whole thing again and see what happens okay so it's starting to upload all of the files over here and you can see the drive id over here as well i'm gonna let this whole thing run and then i'll open up google drive and see if all this actually worked or not okay so it's done let's open up google drive now here i am in the google drive and you can see that it uploaded this drive syncer video folder with all of the files you can see all of them over here that was successfully uploaded yeah everything works perfect all of the files got uploaded successfully awesome now uh do we still have time yes we still have time I try to limit all of my videos to like 15 20 minutes in total so they're not too long to watch for you guys anyway let's see let's see what can we do now about this actually let's create the base folder as well so let me let me walk you through what I mean over here so in the config we have this base folder right this is a string this is basically a folder name and the purpose the base folder is that i want to put all of the projects that i synchronize with this application in inside a single folder right i don't want them scattered across my google drive everywhere and i have to look for them and all that i want all of them inside one single base folder organized neatly so that i can actually you know find them easily so let's configure this let's implement this this is going to be pretty simple but there are a couple of new concepts we need to learn Let create a function over here Create base folder drive This will take the drive struct and it will return a variable called base which will be drive dot file this will be the base folder that we create right now let implement this first of all i'm going to create a variable called new base over here you'll see why i named this new base in just a second this will be a drive dot file it'll take a name which will be in drive dot config dot base folder that's the name of this folder that we want to have it'll take the mime type this right here, application slash vnd.google-apps.folder. This is the mime type of every single folder on Google Drive. If you want to create a folder on Google Drive, this is the mime type that you pass in and it will create that folder for you. Then let's just do something like r for response and error is equal to drive.api.files.create. There we go. pass in the new base over here as a reference run do and then let's handle the error let's say log.fatal f fail to create base folder print the base folder over here print the error add a new line the base folder will be in drive.config.base folder there we go and then print the error as well there we go and again i'm using fatal f to crash the program if we if we can't create the base folder right if there's an error in trying to create the break base folder why am i doing this because if I can't even create the base folder then what purpose is it to try to upload the folders right because all of these folders all of these projects I want to upload inside the base folder right if I can't even create that then there's no reason to keep the program running right which is why I use fatal F to just exit the program if we get an error over here anyway then we can just say say something like base equals R This is the base variable up here and we can just return base over here In fact, we can even skip that part and just do return our it's up to you Honestly now the reason why I named this new base is because there's there's two parts to this function to this create base folder function right over here The first part is we want to create the base folder Okay, the second part is that we want to also also first see whether that folder already exists or not, right? Because if it already exists, this is going to create a duplicate folder. Google Drive does not discriminate between folders by their names. It does it by their ID, right? So every single file and folder on Google Drive has an ID, something like all of these weird strings you see over here, right? Every single file and folder on Google Drive is going to have a unique ID like this, which means that folders can have the same name and it will just create duplicate folders right we don't want that so what we're going to do is we're going to first see if the folder already exists all right and we can do that by running a query so let's create a query over here this will be a string let's do fmt.sprintf because i'm going to need a bunch of variables over here we're going to say mime type equals And by the way, since I have to type this whole mime type out so many times, what I should do is I should extract it into some sort of a constant over here. So I believe you can just do something like constant folder mime type and just create something like this, right? And then we can just pass that in over here and over here as well. There we go. And let me just make sure that this doesn't exist anywhere else by searching over here. and you can see this is the only place that this string exists okay perfect now let's go back over here in the query we want to make sure that this has a mime type of print a string over here folder mime type there we go and make sure this is wrapped in single quotes as you can see over here and name equals again wrapped in single quotes and prints a string over here this will be the drive.config.base folder basically the folder name okay that's all and trash equals false so basically it's not in the trash folder it hasn't been deleted basically all i'm saying over here this is a search query basically is the mime type must be the folder mime type which means this must be a folder this cannot be a file named programming right programming is the um base folder name over here that I'm using right obviously you can use whatever you want but the mime type must be a folder this must be a folder that we're searching for not a file and the name of this folder must match the config based config dot base folder that we have over here and it must not be in the trash or in the recycle bin right that's what I'm saying over here now we can run this query and search for the folder we can say something like drive.api.files.list.q q is what you use to pass in a query and then we can also use fields over here fields will basically allow us to limit the number of um attributes we get back right we can say something like files id and name because that's all we really need to search for right now right and then we can just run do to run the entire function then this will return a response and an error make sure to handle the error over here let's say fail to i forgot to actually run the function log dot print f fail to query base folder cannot proceed with synchronization let just say that print the error over here and it should be it should be a log dot fatal F not print F and then we can also run something like we can add a check over here a check like if our dot file if length of our dot files is greater than one then that means there are multiple folders that match this query right we don't want that we want only one base folder because if you have multiple base folders which one are you going to upload all of the projects projects to right so um let's just say something like found multiple base folder matches right and if there's multiple folders like this which one do we upload to right so ideally this will only have one match right and we can get that one match by just looping over it or directly accessing it like this i prefer to loop over it so you can say something like for underscore f range f r dot files we can add another check over here this is not really necessary but i like to read thorough so yeah make sure that the names match and then if they do just return f over here and that's all we need to do now this is giving me an error over here because these variables are already defined so remove this colon and it should work and it's still giving me an error cannot use this as drive file this value in assignment you know what let's just rename this whole thing let's just say f over here instead of r and return f over here there we go and i guess we can add the colon back and that fixes the problem awesome so this function is going to create the base folder for us now we can go down here in the upload folder function and before we do anything else we should create the base folder so we'll run this function we'll pass in the drive this will return a variable a base which will be a drive.file and then we can pass that in over here where we create the project folder right so we can say parents and the parents of this project folder should be the base folder right uh base folder id there we go and that's all we need to do over here now first of all let me just delete everything on my google drive right and now let's run everything again let's clear the screen let's run and see what happens it started the upload process for this folder and it started uploading all of the files let's wait for this to finish first and then we'll see what happens okay here we are in my Google Drive you can see created this programming folder and it created this drive sync or video folder inside of it and here are all of the files that we had right I can open one of it which one do I open main.go and you can see all of the code over here so it uploaded successfully right config.go and so on and so forth right all of the code is over here so this works and it got this whole folder was placed inside of this base folder programming where i want to store all of my projects which means now i can just go over to config.json and in this folders list over here i can just add as many folders as i want if i have 20 side projects I can just add all of them over here and it'll just upload all of them inside this programming folder which is kind of awesome when you think about it right everything's going to be neatly organized for us and because we're building a custom application over here we can basically do whatever we want with this right we can add we can add all sorts of features over here to make our life easier anyway with that this video just got way too long all of a sudden so we're going to stop over here. After editing all of this, this will probably end up being a 25-26 minute video, which is fairly long, but I think you guys can keep up with it, right? Hopefully, you don't have the attention span of a goldfish. Hopefully, you don't have TikTok brains where you're just looking for the next video, right? Hopefully, you can take a 27-minute video. So, we built the basic functionality to upload files and folders over here. In the next video, we're probably going to expand on this and build a couple more features stuff like um reading the the git ignore files so that we don't upload all of the sensitive files that we want to store locally we don't upload them to google drive um and other stuff as well we're going to add a bunch more features to this i've already got all of them planned and i'm gonna keep building um keep recording videos and keep building more features so we'll see how that goes and again if you have any questions about any of this code if you don't understand some part of it if you if you have any questions regarding all of that you should leave those in the comments below a couple of you guys do ask questions and i make sure to answer all of them but most of you don't which is kind of weird like if you have any questions you should ask in the comments below so that i can help you out and i can teach you whatever is missing right like you you guys just gotta talk more you know you guys you need to leave more comments you need to start conversations you need to talk more you know like you see a cute girl and you instead of you going up to talk to her you just stare at her from far away that's what you guys do when you don't leave comments so make sure you leave a comment right make sure you go talk to the girl i mean make sure you leave a comment and ask any questions if you have any or just tell me i'm doing a great job because i think i am but who knows right Anyway, I'm just rambling at this point. Let's end the video. It's already too long. Thank you for watching. Thank you so much for following along with this series. Like, comment, subscribe. Do all of those wonderful things. And I will see you in the next video, which will be part 4 of this. Bye bye.