aboutsummaryrefslogtreecommitdiffstats
path: root/util
ModeNameSize
-rw-r--r--migrate_address_to_pubkey.coffee329logstatsplainblame
ys <[email protected]> 2022-10-17 13:31:59 -0700 process uploads to webp' href='/coinos-server.git/commit/lib/upload.js?h=staging&id=3afcfd03edbafa489e57c85a4956262d92f2f7d1'>3afcfd03 ^
f0543b6d ^
a81cb5ad ^







7709c1ef ^
a81cb5ad ^









4ce44144 ^
f0543b6d ^

b67ca07b ^
f0543b6d ^
5955ff69 ^
a81cb5ad ^
06b59b58 ^
f0543b6d ^
e918a818 ^

06b59b58 ^
e918a818 ^
06b59b58 ^
e918a818 ^

06b59b58 ^
7709c1ef ^
3afcfd03 ^
a81cb5ad ^
7709c1ef ^
a81cb5ad ^


06b59b58 ^
a81cb5ad ^
e918a818 ^
a81cb5ad ^



7709c1ef ^
a81cb5ad ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63




                                               

                                  
                                   







                                             
                               









                                       
 

                                    
                     
          
 
                                             
 
                              

                                
 
                                                                     
 

                                                                  
 
                                                
 
                                                         
                                                                               


                                                  
 
                     
  



                                           
                                                     



                                                           
import { fileTypeFromStream } from "file-type";
import pump from "pump";
import Clone from "readable-stream-clone";
import fs from "fs";
import path from "path";
import sharp from "sharp";
import { pipeline } from "stream";
import { g, s, db } from "$lib/db";
import crypto from "crypto";

async function getSha256Hash(stream) {
  return new Promise((resolve, reject) => {
    const hash = crypto.createHash("sha256");
    stream.pipe(hash);

    let hashValue;
    hash.on("data", (data) => {
      hashValue = data.toString("hex");
    });

    hash.on("end", () => {
      resolve(hashValue);
    });

    hash.on("error", reject);
  });
}

export default async (req, res) => {
  let {
    params: { type },
  } = req;

  let width = type === "banner" ? 1920 : 240;

  let data = await req.file();
  let s1 = new Clone(data.file);
  let s2 = new Clone(data.file);

  let [format, ext] = (await fileTypeFromStream(s1)).mime.split("/");

  if (format !== "image" && !["jpg", "jpeg", "png"].includes(ext))
    throw new Error("unsupported file type");

  let t = sharp().rotate().resize(width).webp();

  let processedBuffer = await streamToBuffer(s2.pipe(t));
  let hash = crypto.createHash("sha256").update(processedBuffer).digest("hex");

  let filePath = `/app/data/uploads/${hash}.webp`;
  fs.writeFileSync(filePath, processedBuffer);

  res.send({ hash });
};

function streamToBuffer(stream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    stream.on("data", (chunk) => chunks.push(chunk));
    stream.on("end", () => resolve(Buffer.concat(chunks)));
    stream.on("error", reject);
  });
}