Skip to content

Commit

Permalink
fix: Change function to return File instead of Blob
Browse files Browse the repository at this point in the history
  • Loading branch information
KantaHasegawa committed Jul 12, 2023
1 parent d933bc8 commit d4f5df6
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions coverage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ <h1>All files</h1>


<div class='fl pad1y space-right2'>
<span class="strong">70.83% </span>
<span class="strong">70.37% </span>
<span class="quiet">Branches</span>
<span class='fraction'>153/216</span>
<span class='fraction'>152/216</span>
</div>


Expand Down Expand Up @@ -85,8 +85,8 @@ <h1>All files</h1>
</td>
<td data-value="88.13" class="pct high">88.13%</td>
<td data-value="430" class="abs high">379/430</td>
<td data-value="70.83" class="pct medium">70.83%</td>
<td data-value="216" class="abs medium">153/216</td>
<td data-value="70.37" class="pct medium">70.37%</td>
<td data-value="216" class="abs medium">152/216</td>
<td data-value="92.85" class="pct high">92.85%</td>
<td data-value="42" class="abs high">39/42</td>
<td data-value="89.61" class="pct high">89.61%</td>
Expand Down Expand Up @@ -116,7 +116,7 @@ <h1>All files</h1>
<div class='footer quiet pad2 space-top1 center small'>
Code coverage generated by
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
at 2023-03-06T13:46:27.026Z
at 2023-07-12T16:04:59.831Z
</div>
<script src="prettify.js"></script>
<script>
Expand Down
10 changes: 5 additions & 5 deletions coverage/lcov-report/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ <h1>All files</h1>


<div class='fl pad1y space-right2'>
<span class="strong">70.83% </span>
<span class="strong">70.37% </span>
<span class="quiet">Branches</span>
<span class='fraction'>153/216</span>
<span class='fraction'>152/216</span>
</div>


Expand Down Expand Up @@ -85,8 +85,8 @@ <h1>All files</h1>
</td>
<td data-value="88.13" class="pct high">88.13%</td>
<td data-value="430" class="abs high">379/430</td>
<td data-value="70.83" class="pct medium">70.83%</td>
<td data-value="216" class="abs medium">153/216</td>
<td data-value="70.37" class="pct medium">70.37%</td>
<td data-value="216" class="abs medium">152/216</td>
<td data-value="92.85" class="pct high">92.85%</td>
<td data-value="42" class="abs high">39/42</td>
<td data-value="89.61" class="pct high">89.61%</td>
Expand Down Expand Up @@ -116,7 +116,7 @@ <h1>All files</h1>
<div class='footer quiet pad2 space-top1 center small'>
Code coverage generated by
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
at 2023-03-06T13:46:27.066Z
at 2023-07-12T16:04:59.862Z
</div>
<script src="prettify.js"></script>
<script>
Expand Down
2 changes: 1 addition & 1 deletion dist/browser-image-compression.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser-image-compression.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser-image-compression.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser-image-compression.mjs.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/image-compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
* @param {boolean} [options.alwaysKeepResolution=false]
* @param {AbortSignal} [options.signal]
* @param {number} previousProgress - for internal try catch rerunning start from previous progress
* @returns {Promise<File | Blob>}
* @returns {Promise<File>}
*/
export default async function compress(file, options, previousProgress = 0) {
let progress = previousProgress;
Expand Down Expand Up @@ -137,5 +137,6 @@ export default async function compress(file, options, previousProgress = 0) {
cleanupCanvasMemory(origCanvas);

setProgress(100);
compressedFile = new File([compressedFile], file.name, { type: file.type });
return compressedFile;
}
11 changes: 5 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const CustomFileReader = (isBrowser || inWebWorker) && ((moduleMapper &&
* @param {string} dataUrl
* @param {string} filename
* @param {number} [lastModified=Date.now()]
* @returns {Promise<File | Blob>}
* @returns {Promise<File>}
*/
export function getFilefromDataUrl(dataUrl, filename, lastModified = Date.now()) {
return new Promise((resolve) => {
Expand All @@ -29,9 +29,8 @@ export function getFilefromDataUrl(dataUrl, filename, lastModified = Date.now())
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
const file = new Blob([u8arr], { type: mime });
file.name = filename;
file.lastModified = lastModified;
const blobFile = new Blob([u8arr], { type: mime });
const file = new File([blobFile], filename, { lastModified, type: blobFile.type });
resolve(file);

// Safari has issue with File constructor not being able to POST in FormData
Expand Down Expand Up @@ -251,7 +250,7 @@ export async function drawFileInCanvas(file, options = {}) {
* @param {string} fileName
* @param {number} fileLastModified
* @param {number} [quality]
* @returns {Promise<File | Blob>}
* @returns {Promise<File>}
*/
export async function canvasToFile(canvas, fileType, fileName, fileLastModified, quality = 1) {
let file;
Expand Down Expand Up @@ -280,7 +279,7 @@ export async function canvasToFile(canvas, fileType, fileName, fileLastModified,
const dataUrl = canvas.toDataURL(fileType, quality);
file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified);
}
return file;
return new File([file], fileName, { lastModified: file.lastModified, type: file.type });
}

/**
Expand Down

0 comments on commit d4f5df6

Please sign in to comment.