-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate user profile backgrounds from MediaCrush
- Loading branch information
Showing
6 changed files
with
110 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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 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 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,30 @@ | ||
import requests | ||
import os | ||
import sys | ||
import subprocess | ||
from werkzeug.utils import secure_filename | ||
from KerbalStuff.objects import User | ||
from KerbalStuff.config import _cfg | ||
from KerbalStuff.database import db | ||
|
||
def download_bg(url, path): | ||
sys.stdout.write("\rDownloading {0}...".format(path)) | ||
subprocess.call(['wget', '--output-document=' + path, url]) | ||
sys.stdout.write("\n") | ||
|
||
total = User.query.count() | ||
for index, user in enumerate(User.query.all()): | ||
if user.backgroundMedia: | ||
print("Handling {} ({} of {})".format(user.username, index + 1, total)) | ||
|
||
filetype = os.path.splitext(os.path.basename(user.backgroundMedia))[1] | ||
filename = secure_filename(user.username) + filetype | ||
base_path = os.path.join(secure_filename(user.username) + '_' + str(user.id)) | ||
full_path = os.path.join(_cfg('storage'), base_path) | ||
if not os.path.exists(full_path): | ||
os.makedirs(full_path) | ||
path = os.path.join(full_path, filename) | ||
|
||
download_bg('https://vox.mediacru.sh/' + user.backgroundMedia, path) | ||
user.backgroundMedia = os.path.join(base_path, filename) | ||
db.commit() |
This file contains 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 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,46 +1,42 @@ | ||
(function() { | ||
window.upload_bg = function(files, box) { | ||
var file, p, progress; | ||
var file, formdata, p, progress, xhr; | ||
file = files[0]; | ||
p = document.createElement('p'); | ||
p.textContent = 'Uploading...'; | ||
p.className = 'status'; | ||
box.appendChild(p); | ||
box.querySelector('a').classList.add('hidden'); | ||
progress = box.querySelector('.upload-progress'); | ||
return MediaCrush.upload(file, function(media) { | ||
progress.classList.add('fade-out'); | ||
progress.style.width = '100%'; | ||
p.textContent = 'Processing...'; | ||
return media.wait(function() { | ||
return MediaCrush.get(media.hash, function(media) { | ||
var path, _i, _len, _ref; | ||
p.textContent = 'Done'; | ||
path = null; | ||
_ref = media.files; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
file = _ref[_i]; | ||
if (file.type === 'image/png' || file.type === 'image/jpeg') { | ||
path = file; | ||
} | ||
} | ||
if (path === null) { | ||
return p.textContent = 'Please upload images only.'; | ||
} else { | ||
document.getElementById('backgroundMedia').value = path.file; | ||
document.getElementById('header-well').style.backgroundImage = 'url("https://mediacru.sh/' + path.file + '")'; | ||
return setTimeout(function() { | ||
box.removeChild(p); | ||
return box.querySelector('a').classList.remove('hidden'); | ||
}, 3000); | ||
} | ||
}); | ||
}); | ||
}, function(e) { | ||
xhr = new XMLHttpRequest(); | ||
xhr.open('POST', "/api/user/" + window.username + "/update-bg"); | ||
xhr.upload.onprogress = function(e) { | ||
if (e.lengthComputable) { | ||
return progress.style.width = (e.loaded / e.total) * 100 + '%'; | ||
} | ||
}); | ||
}; | ||
xhr.onload = function(e) { | ||
var resp; | ||
if (xhr.status !== 200) { | ||
p.textContent = 'Please upload JPG or PNG only.'; | ||
return setTimeout(function() { | ||
box.removeChild(p); | ||
return box.querySelector('a').classList.remove('hidden'); | ||
}, 3000); | ||
} else { | ||
resp = JSON.parse(xhr.responseText); | ||
p.textContent = 'Done!'; | ||
document.getElementById('backgroundMedia').value = resp.path; | ||
document.getElementById('header-well').style.backgroundImage = 'url("' + resp.path + '")'; | ||
return setTimeout(function() { | ||
box.removeChild(p); | ||
return box.querySelector('a').classList.remove('hidden'); | ||
}, 3000); | ||
} | ||
}; | ||
formdata = new FormData(); | ||
formdata.append('image', file); | ||
return xhr.send(formdata); | ||
}; | ||
|
||
}).call(this); |
This file contains 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