Skip to content

Commit

Permalink
http range support
Browse files Browse the repository at this point in the history
  • Loading branch information
imzlh committed Sep 30, 2024
1 parent 1a5905c commit 6237c19
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 9 deletions.
104 changes: 104 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: Release

env:
YARN_INSTALL_NOPT: yarn add --ignore-platform --ignore-optional

on:
push:
branches:
- master
tags:
- v*
workflow_dispatch:

jobs:
release:
runs-on: ${{ matrix.os }}

strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-22.04]

steps:
- name: Check out Git repository
uses: actions/checkout@v3
with:
submodules: "recursive"

- name: Install Node.js, NPM and Yarn
uses: actions/setup-node@v3
with:
node-version: 20
cache: 'yarn'

- name: Install RPM & Pacman (on Ubuntu)
if: runner.os == 'Linux'
run: |
sudo apt-get update &&
sudo apt-get install --no-install-recommends -y rpm &&
sudo apt-get install --no-install-recommends -y libarchive-tools &&
sudo apt-get install --no-install-recommends -y libopenjp2-tools
- name: Install Snapcraft (on Ubuntu)
uses: samuelmeuli/action-snapcraft@v1
if: startsWith(matrix.os, 'ubuntu')
with:
snapcraft_token: ${{ secrets.snapcraft_token }}

- id: get_unm_version
name: Get the installed UNM version
run: |
yarn --ignore-optional
unm_version=$(node -e "console.log(require('./node_modules/@unblockneteasemusic/rust-napi/package.json').version)")
echo "::set-output name=unmver::${unm_version}"
shell: bash

- name: Install UNM dependencies for Windows
if: runner.os == 'Windows'
run: |
${{ env.YARN_INSTALL_NOPT }} \
@unblockneteasemusic/rust-napi-win32-x64-msvc@${{steps.get_unm_version.outputs.unmver}}
shell: bash

- name: Install UNM dependencies for macOS
if: runner.os == 'macOS'
run: |
${{ env.YARN_INSTALL_NOPT }} \
@unblockneteasemusic/rust-napi-darwin-x64@${{steps.get_unm_version.outputs.unmver}} \
@unblockneteasemusic/rust-napi-darwin-arm64@${{steps.get_unm_version.outputs.unmver}} \
dmg-license
shell: bash

- name: Install UNM dependencies for Linux
if: runner.os == 'Linux'
run: |
${{ env.YARN_INSTALL_NOPT }} \
@unblockneteasemusic/rust-napi-linux-x64-gnu@${{steps.get_unm_version.outputs.unmver}} \
@unblockneteasemusic/rust-napi-linux-arm64-gnu@${{steps.get_unm_version.outputs.unmver}} \
@unblockneteasemusic/rust-napi-linux-arm-gnueabihf@${{steps.get_unm_version.outputs.unmver}}
shell: bash

- name: Build/release Electron app
uses: samuelmeuli/[email protected]
with:
github_token: ${{ secrets.github_token }}
release: ${{ startsWith(github.ref, 'refs/tags/v') }}
use_vue_cli: true

- uses: actions/upload-artifact@v3
with:
name: YesPlayMusic-mac
path: dist_electron/*-universal.dmg
if-no-files-found: ignore

- uses: actions/upload-artifact@v3
with:
name: YesPlayMusic-win
path: dist_electron/*Setup*.exe
if-no-files-found: ignore

- uses: actions/upload-artifact@v3
with:
name: YesPlayMusic-linux
path: dist_electron/*.AppImage
if-no-files-found: ignore
58 changes: 49 additions & 9 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createServer } from 'http'
import { createReadStream } from 'fs'
import { FakeNgxResponse } from './polyfill'
import API from './server';
import { stat } from 'fs/promises'

app.whenReady().then(() => {
electronApp.setAppUserModelId('com.electron')
Expand Down Expand Up @@ -55,6 +56,7 @@ ipcMain.addListener('create-session', (_: Event, search: string) => {
width: 1200,
height: 800,
autoHideMenuBar: true,
icon: require.resolve('@imzlh/vlist/dist/favicon.ico'),
webPreferences: {
nodeIntegration: true,
sandbox: false,
Expand All @@ -63,33 +65,71 @@ ipcMain.addListener('create-session', (_: Event, search: string) => {
preload: join(__dirname, '../preload/index.js')
}
});
newWin.loadFile('node_modules/@imzlh/vlist/dist/index.html', {
newWin.loadFile(require.resolve('@imzlh/vlist/dist/index.html'), {
search
});
windows.push(newWin);
newWin.show();
})
ipcMain.addListener('create-server', (_, path: string) => {
// 创建服务器
const server = createServer((req, res) => {
const server = createServer(async (req, res) => {
const url = new URL(req.url!, `http://${req.headers.host}`);
console.log(req.method, url.pathname);
if (url.pathname === '/@api/') {
const vhandle = new FakeNgxResponse(res);
vhandle.__waitReady()
.then(() => API(vhandle, path))
.catch(e => console.error(e));
try{
await vhandle.__waitReady()
await API(vhandle, path);
}catch(e){
console.error(e);
if(!res.writableFinished)
vhandle.return(403, String(e));
}
}else{
// 设置静态文件目录
const filePath = join(path, url.pathname);
const filePath = join(path, decodeURIComponent(url.pathname));

let stats;
try{
stats = await stat(filePath);
}catch(e){
res.statusCode = 404;
res.end(String(e));
return;
}

const range = [0, stats.size];
if(req.headers.range){
const match = req.headers.range.match(/bytes=(\d+)-(\d*)/);
if(match){
if(match[1]) range[0] = parseInt(match[1]);
if(match[2]) range[1] = parseInt(match[2]) +1;

if(range[0] < 0 || range[1] > stats.size || range[0] >= range[1]){
res.statusCode = 416;
return res.end('Requested Range Not Satisfiable');
}

res.statusCode = 206;
res.setHeader('Content-Range', `bytes ${range[0]}-${range[1]-1}/${stats.size}`);
}else{
res.statusCode = 416;
return res.end('invalid range format');
}
}

try{
res.statusCode = 200;
res.setHeader('Content-Length', range[1] - range[0]);
res.setHeader('Content-Type', 'application/octet-stream');
createReadStream(filePath).pipe(res);
createReadStream(filePath, {
"start": range[0],
"end": range[1]
}).pipe(res);
}catch(e){
res.statusCode = 404;
res.end();
res.end(String(e));
}
}
});
Expand All @@ -112,7 +152,7 @@ ipcMain.addListener('create-server', (_, path: string) => {
preload: join(__dirname, '../preload/index.js')
}
});
newWin.loadFile('node_modules/@imzlh/vlist/dist/index.html', {
newWin.loadFile(require.resolve('@imzlh/vlist/dist/index.html'), {
"search": `?api=http://${addrstr}/@api/&proxy=http://${addrstr}`
});
newWin.on('closed', () => server.close());
Expand Down

0 comments on commit 6237c19

Please sign in to comment.