laitimes

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

author:CSDN
Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

The author of this article decided to use a JavaScript script to clean up his iCloud under the comprehensive cost factor, but he found an unexpected discovery, that is, after uploading the same video in iCloud and deleting this video, the iCloud space is not the same.

Original: https://andykong.org/blog/icloudconfusion

This article is a translation of CSDN and is not allowed to be reproduced without permission.

作者 | Andy Kong 翻译 | 苏宓

Produced by | CSDN(ID:CSDNnews)

Recently, I received an email from Apple informing me that my iCloud storage is full.

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

In the email, Apple gave me an option to pay for the upgrade to get more iCloud space. However, I found that the cost difference between 1TB and 200GB was tripled, and some were too expensive, which prompted me to start looking for alternatives. When I turn on iCloud, I find that photos take up most of the storage space (~127GB), so I guess I can go into my photo album and sort by file size, and then delete it appropriately, freeing up some space.

Unfortunately, Apple devices don't support this feature. Whatever the reason, I still want to see my photos by file size. There are several apps out there that can view photos and highlight duplicate photos, or view videos and show the file size. The only problem is that photos that have already been uploaded to iCloud don't show up in these apps, so they're not really useful. Also, do you really want a random app to scan all your photos?

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!
Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

JavaScript as a solution

So, I went to the iCloud website and thought about the possibilities. I navigated to the "Photos" -> "Media Type" -> the "Video" menu bar.

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

As you can see, each video has a corresponding duration box - as long as it's an HTML element, we can use JS to search and filter them. I've found that each duration box has a video-text-badge class. This way, we can find all the videos in the page, sort them by their duration, and highlight the ones that exceed a certain threshold. Here's the code:

// From the blog post andykong.org/blog/icloudconfusion/

function boxTops(){
 HTMLCollection.prototype.toArray = function() { return Array.from(this); }

// Select all the time badges and parse out their total runtime in seconds
 a = document.getElementsByClassName('video-text-badge').toArray()
 b = a.map((x) => x.innerText)
 c = b.map((y) => y.split(":").map((x) => parseInt(x)))
 d = c.map((x) => x[0]*60 + x[1])

// Sort the badges HTML array and badges runtime together
 indices = Array.from(a.keys())
 indices.sort( (x,y) => d[x] - d[y])

 sortedA = indices.map(i => a[i])
 sortedD = indices.map(i => d[i])


// Function that boxes an element
function drawBox(element) {
if (element instanceof HTMLElement) {
 element.style.border = "2px solid red";
 }
 }

 thresh = 30 // Min seconds to highlight a video
for (let i = 0; i < sortedD.length; i++) {
if (sortedD[i] > thresh){
 drawBox(sortedA[i])
 }
 }
}

// Continuously highlight big videos
setInterval(boxTops, 500)           

Because iCloud only loads elements on the page, I made it a function that runs on a timer so that new elements are highlighted when scrolling into it. It looks like this:

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

Videos larger than 20 seconds are circled in red to make them easier to identify

To use it, simply open the Javascript console (right-click on the page -> inspect the element) and paste the entire gist. Now you can easily select multiple large videos from iCloud and download them before deleting them, moving them to long-term storage: hard drives, other cloud storage, and more.

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

Mystery

I used this script to delete all videos older than 30 seconds in iCloud. Interestingly, after I deleted all the "big videos" and downloaded them, it cleared ~55GB of content from my iCloud despite only downloading 7GB of videos. Here's the answer.

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

All downloaded videos take up 8GB of disk space

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

iCloud storage is reduced from 199GB to 143GB after downloading 7GB of video

For some reason, the 7GB video takes up far more space in the cloud than on my hard drive. Interestingly...

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

Experiment 1

I'd like to test it further. First, I uploaded a 4K video. It takes up 281 MB. After uploading, my storage looks like this:

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

Upload a 4K video and iCloud shows 145.33 GB used

Then I downloaded and deleted it. The file is still 281 MB. Here's what the storage looks like after that:

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

The 4K video was deleted, and iCloud said it had used 145.6 GB

Deleting a 281MB video frees up ~270MB of space. This is puzzling. What about other older videos?

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

Experiment 2

I thought, maybe an older video might have multiple copies saved in iCloud, so I searched my videos to see if I could find one that was taking up a lot of storage space. I found a video with a lot of charts and iCloud says it takes up 128 MB.

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

Older large videos, which take up 128 MB

When downloaded, the file is only 47 MB!

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

The downloaded video file is 47 MB

Here's how my iCloud storage compares before and after:

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

iCloud storage before deleting old videos, 145.29 GB has been used

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

iCloud storage after deleting old videos, 145.12 GB has been used, 170MB less

So, iCloud shows that the video is 128MB, and after I download it, I find that the video is actually 48MB, and after deleting the video, my available storage space has increased by about 170MB. It's so interesting!

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!

conclusion

My storage space is freeing up more than 7 times the size of the deleted files, which is weird. What's even stranger is that the large capacity of old videos seems to take up much more storage space in iCloud than in real life.

Do you know why this is happening?

Cleaned up my 200GB iCloud with JavaScript and had an unexpected discovery!