This repository has been archived by the owner on Nov 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
145 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,3 +197,17 @@ export function refreshCurrentUserInfo() { | |
} | ||
); | ||
} | ||
|
||
/** | ||
* 上传用户主页背景图 | ||
* @author mutoe <[email protected]> | ||
* @param {File} file 图像文件 | ||
*/ | ||
export function uploadUserBanner(file) { | ||
const formData = new FormData(); | ||
formData.append("image", file); | ||
return api.post(`/user/bg`, formData, { | ||
headers: { "Content-Type": "multipart/form-data" }, | ||
validateStatus: s => s === 204 | ||
}); | ||
} |
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 |
---|---|---|
|
@@ -5,8 +5,7 @@ | |
:key='img.src' | ||
v-for='(img, index) in pics' | ||
class="m-box-center m-box-center-a image-wrap" | ||
:class="[picClass, { error: img.error }, { loading: img.loading }, {edit}]" | ||
> | ||
:class="[picClass, { error: img.error }, { loading: img.loading }, { edit }]"> | ||
<div class="image-placeholder"></div> | ||
<img | ||
:id="`compose-photo-${img.id}`" | ||
|
@@ -74,69 +73,8 @@ import bus from "@/bus.js"; | |
import { mapActions } from "vuex"; | ||
import sendImage from "@/util/SendImage.js"; | ||
import ImagePaidOption from "./ImagePaidOption.vue"; | ||
/** | ||
* ReadAsArrayBuffer | ||
* 通过文件头判断文件格式 | ||
* @author jsonleex <[email protected]> | ||
* @param {[type]} file | ||
* @return {[type]} | ||
*/ | ||
function readAsArrayBuffer(file) { | ||
return new Promise(resolve => { | ||
const reader = new FileReader(); | ||
reader.onloadend = event => { | ||
const uint8 = new Uint8Array(event.target.result).subarray(0, 4); | ||
let res = ""; | ||
for (let i = 0; i < uint8.length; i++) { | ||
res += uint8[i].toString(16); | ||
} | ||
import { checkImageType } from "@/util/imageCheck.js"; | ||
let mimeType = ""; | ||
switch (res) { | ||
case "89504e47": | ||
mimeType = "png"; | ||
break; | ||
case "47494638": | ||
mimeType = "gif"; | ||
break; | ||
case "52494646": | ||
mimeType = "webp"; | ||
break; | ||
default: | ||
res.indexOf("424d") === 0 | ||
? (mimeType = "bmp") | ||
: res.indexOf("ffd8ffe") === 0 && (mimeType = "jpeg"); | ||
} | ||
file.mimeType = mimeType; | ||
resolve(mimeType); | ||
}; | ||
reader.readAsArrayBuffer(file); | ||
}); | ||
} | ||
/** | ||
* Check image types | ||
* @author jsonleex <[email protected]> | ||
* @param {[type]} files | ||
* @return {[type]} | ||
*/ | ||
function checkImageType(files) { | ||
return new Promise((resolve, reject) => { | ||
const exts = ["png", "jpg", "jpeg", "gif", "bmp", "webp"]; | ||
const blobs = []; | ||
for (let index = 0; index < files.length; index++) { | ||
const fileName = files[index].name.split("."); | ||
if (fileName.length > 1) { | ||
const ext = fileName.pop().toLowerCase(); | ||
exts.indexOf(ext) < 0 | ||
? reject(new Error("不支持的文件格式")) | ||
: (files[index].mimeType = ext); | ||
} else { | ||
blobs.push(readAsArrayBuffer(files[index])); | ||
} | ||
} | ||
resolve(files); | ||
}); | ||
} | ||
export default { | ||
name: "image-list", | ||
components: { | ||
|
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,63 @@ | ||
/** | ||
* ReadAsArrayBuffer | ||
* 通过文件头判断文件格式 | ||
* @author jsonleex <[email protected]> | ||
* @param {[type]} file | ||
* @return {[type]} | ||
*/ | ||
export function readAsArrayBuffer(file) { | ||
return new Promise(resolve => { | ||
const reader = new FileReader(); | ||
reader.onloadend = event => { | ||
const uint8 = new Uint8Array(event.target.result).subarray(0, 4); | ||
let res = ""; | ||
for (let i = 0; i < uint8.length; i++) { | ||
res += uint8[i].toString(16); | ||
} | ||
|
||
let mimeType = ""; | ||
switch (res) { | ||
case "89504e47": | ||
mimeType = "png"; | ||
break; | ||
case "47494638": | ||
mimeType = "gif"; | ||
break; | ||
case "52494646": | ||
mimeType = "webp"; | ||
break; | ||
default: | ||
res.indexOf("424d") === 0 | ||
? (mimeType = "bmp") | ||
: res.indexOf("ffd8ffe") === 0 && (mimeType = "jpeg"); | ||
} | ||
file.mimeType = mimeType; | ||
resolve(mimeType); | ||
}; | ||
reader.readAsArrayBuffer(file); | ||
}); | ||
} | ||
/** | ||
* Check image types | ||
* @author jsonleex <[email protected]> | ||
* @param {[type]} files | ||
* @return {[type]} | ||
*/ | ||
export function checkImageType(files) { | ||
return new Promise((resolve, reject) => { | ||
const exts = ["png", "jpg", "jpeg", "gif", "bmp", "webp"]; | ||
const blobs = []; | ||
for (let index = 0; index < files.length; index++) { | ||
const fileName = files[index].name.split("."); | ||
if (fileName.length > 1) { | ||
const ext = fileName.pop().toLowerCase(); | ||
exts.indexOf(ext) < 0 | ||
? reject(new Error("不支持的文件格式")) | ||
: (files[index].mimeType = ext); | ||
} else { | ||
blobs.push(readAsArrayBuffer(files[index])); | ||
} | ||
} | ||
resolve(files); | ||
}); | ||
} |