Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.0 kB
2
Indexable
Never
const app = require("./app");
const fs = require('fs');
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);


const AWS = require('aws-sdk');
const path = require('path');

// Cloudflare R2 yapılandırması
const s3 = new AWS.S3({
    endpoint: 'https://aaa780ca2d934ac0f129acd5a54e5c39.r2.cloudflarestorage.com/test',
    accessKeyId: '727af0b948e0c85d561d8e0bef1c6404',
    secretAccessKey: '5af4672a710f4119f0cbbb95629f14b7e6d8ec89198a66ce5fe083bbb93797ce',
    signatureVersion: 'v4',
    region: 'auto'
});


io.on('connection', (socket) => {
  const chunks = [];

  socket.on('videoChunk', (data) => {
    console.log('chunk geldi');
    chunks.push(data);
    socket.emit('videoChunkCompleted', chunks.length);
  });

  socket.on('getVideoLink', async (data) => {

    const fileName = `${date.getFullYear()}${date.getMonth()+1}${date.getDate()}_${date.getHours()}${date.getMinutes()}${date.getSeconds()}.webm`;
    const videoBuffer = Buffer.concat(chunks);
    fs.writeFileSync(fileName, videoBuffer);
    const file = fs.readFileSync('output.mp4');

    const formData = new FormData();
    console.log(file);
    // formData.append('file', file);
    formData.append('file', new Blob([file]), 'output.mp4');

      
    const params = {
      Bucket: 'test',
      Key: 'videoFileName.webm',
      Body: file
    };

    s3.upload(params, function(err, data) {
      if (err) {
          throw err;
      }

      socket.emit('sendVideoLink', `https://r2.recram.com/${data.Bucket}/${data.key}`);
    });

    
    // fetch('https://api.cloudflare.com/client/v4/accounts/aaa780ca2d934ac0f129acd5a54e5c39/stream', {
    //   method: 'POST',
    //   headers: {
    //     'Authorization': 'Bearer ZWsu-xlOT1mRrmfLuuOIhCxCwOP2WEKttjuONZmG'
    //   },
    //   body: formData
    // })
    // .then(response => response.json())
    // .then(res => {

    //   var iframeLink = `https://customer-w93qrztwlhpn8idg.cloudflarestream.com/${res.result.uid}/iframe?preload=true&poster=${encodeURI(res.result.thumbnail)}`;
    //   socket.emit('sendVideoLink', iframeLink);

    // })
    // .catch(error => {
    //   console.log(error);
    //   socket.emit('sendVideoLink', false);
    // });

    // fetch('http://localhost:' + process.env.PORT + '/api/medias/upload', {
    //   method: "POST",
    //   body: formData,
    // })
    // .then(response => response.json())
    // .then(res => {
    //   console.log(res);
    //   socket.emit('sendVideoLink', res.data.mediaPath);
    // })
    // .catch(error => {
    //   socket.emit('sendVideoLink', false);
    // });
    
    
  });

  socket.on('disconnect', () => {

    console.log('disconnected');

  });

});

server.listen(7777, () => {
  console.log(`Socket server is listening on port ${7777}`);
});
Leave a Comment