Skip to content

Commit

Permalink
Ajout du store fonctionnel
Browse files Browse the repository at this point in the history
  • Loading branch information
paxo-rch committed Nov 13, 2024
1 parent 274f3c8 commit 79dda07
Show file tree
Hide file tree
Showing 22 changed files with 1,431 additions and 507 deletions.
12 changes: 11 additions & 1 deletion app/Controllers/Http/DashboardController.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import App from "App/Models/App"

export default class DashboardController {
public async index({ auth, view }: HttpContextContract) {
const user = auth.use('web').user

// show all apps for every user -> print
const apps = await App.query();

for (let i = 0; i < apps.length; i++) {
console.log(apps[i]);
}

console.log(user)
return view.render('core/dashboard', {
user: user,
})
}
}
}
246 changes: 246 additions & 0 deletions app/Controllers/Http/StoreController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import Application from '@ioc:Adonis/Core/Application'
import { v4 as uuidv4 } from 'uuid'
import App from 'App/Models/App'
import User from 'App/Models/User'
import AdmZip from 'adm-zip'
import fs from 'fs'
import path from 'path'

function removeDirectory(directoryPath: string) {
if (fs.existsSync(directoryPath)) {
fs.readdirSync(directoryPath).forEach((file) => {
const curPath = path.join(directoryPath, file)
if (fs.lstatSync(curPath).isDirectory()) {
removeDirectory(curPath)
} else {
fs.unlinkSync(curPath)
}
})
fs.rmdirSync(directoryPath)
}
}


export default class StoreController
{
public async home({ auth, request, view }: HttpContextContract)
{
const user = auth.use('web').user

const app_per_page = 15
const parameters = request.qs()

parameters.page = parameters.page ? parameters.page : 1
parameters.type = parameters.type ? parameters.type : -1

const apps = await App.query()
.orderBy('downloads')
.if(parameters.type !== -1, (query) => {
query.where('category', parameters.type)
})
.offset(app_per_page * (parameters.page - 1))
.limit(app_per_page)

const users = await User.all()

return view.render('store/store', {
apps: apps,
users: users,
user: user
})
}

public async myapps({ auth, response, view }: HttpContextContract)
{
const user = auth.use('web').user

if (!user) {
return response.redirect().toPath('/auth/login/?redirect=/store/myapps')
}

const apps = await App.query()
.where('userId', user.id)

return view.render('store/myapps', {
apps: apps,
user: user
})
}

public async app({ auth, response, request, view }: HttpContextContract)
{
const id = request.params().id

const app = await App.findOrFail(id)

if(!app)
{
return response.redirect('/store')
}

const appuser = await User.findOrFail(app.userId)

const user = auth.use('web').user

//const size =

return view.render('store/app', {
app: app,
appuser: appuser,
user: user
})
}

public async myapp({ auth, response, request, view }: HttpContextContract)
{
const id = request.params().id

const app = await App.findOrFail(id)

if(!app)
{
return response.redirect('/store/myapps')
}

const appuser = await User.findOrFail(app.userId)

const user = auth.use('web').user

if (!user) {
return response.redirect().toPath('/auth/login/?redirect=/store/myapps')
}

if (appuser.id !== user.id) {
return response.redirect().toPath('/store/myapps')
}

return view.render('store/myapp', {
app: app,
appuser: appuser,
user: user
})
}

public async new({ auth, response,view }: HttpContextContract)
{
const user = auth.use('web').user

if (!user) {
return response.redirect().toPath('/auth/login?redirect=/store/new')
}

console.log(user?.username)

return view.render('store/new', {
user: user,
})
}

public async post({ auth, request, response }: HttpContextContract) {
try {
const erase = request.qs().eraseid

const user = auth.use('web').user

if (!user) {
return response.redirect().toPath('/auth/login?redirect=/store/new')
}


const files = request.files('files')

if (!files || files.length === 0) {
console.log('No files uploaded')
return response.badRequest('No files uploaded')
}

const timestamp = new Date().getTime()
const folderPath = `public_apps/${user.id}/${timestamp}`

let app: App
let directorytoremove = ""

if(erase !== undefined)
{
console.log('reusing app ' + erase)

app = await App.findByOrFail('id', erase)
if(!app || app.userId !== user.id)
{
return response.redirect('/store/new')
}
directorytoremove = app.path
}
else
{
app = new App()
console.log('Creating new app')
}

app.userId = user.id;
app.name = request.input('name');
app.desc = request.input('desc');
app.source_url = request.input('source_url');
app.category = request.input('category');
app.path = folderPath + '/' + files[0].clientName.split('/')[0];

await app.save();

if(erase !== undefined)
response.redirect('/store/myapps')
else
response.redirect('/store/apps')

console.log('Starting folder upload')

for (let file of files) {

if (!file.isValid) {
console.log('File upload issue:', file.errors)
continue
}

const fileName = file.clientName

await file.move(Application.publicPath(folderPath), {
name: fileName,
overwrite: true,
})
}

if(erase !== undefined && directorytoremove.length > 0)
{
console.log(directorytoremove)
removeDirectory(Application.publicPath(directorytoremove.split('/').slice(0, -1).join('/')))
}
} catch (error) {
console.error('Folder upload failed:', error);
return response.status(500).send('Folder upload failed');
}
}

public async download({ request, response }: HttpContextContract) {
const id = request.params().id

const app = await App.findOrFail(id)

if(!app)
{
return response.redirect('/store')
}

const directoryPath = Application.publicPath("" + app.path/*.split('/').slice(0, -1).join('/')*/)
const zipFileName = app.name + '.zip'

const zip = new AdmZip()
zip.addLocalFolder(directoryPath)

const zipBuffer = zip.toBuffer()

response.type('application/zip')
response.header('Content-Disposition', `attachment; filename="${zipFileName}"`)

return response.send(zipBuffer)
}
}
2 changes: 1 addition & 1 deletion app/Controllers/Http/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class UsersController {
try {
const user = await auth.verifyCredentials(login, password)
await auth.login(user)
response.redirect().toRoute("dash")
response.redirect().toPath(request.input('redirect', '/'))
} catch {
session.flash({ error: 'Invalid credentials' })
response.redirect().toRoute('auth.login')
Expand Down
4 changes: 2 additions & 2 deletions app/Middleware/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export default class AuthMiddleware {
/**
* Unable to authenticate using any guard
*/
throw new AuthenticationException(
/*throw new AuthenticationException( // intentally removed
'Unauthorized access',
'E_UNAUTHORIZED_ACCESS',
guardLastAttempted,
this.redirectTo,
)
)*/
}

/**
Expand Down
8 changes: 1 addition & 7 deletions app/Models/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export default class App extends BaseModel {
@column({ isPrimary: true })
public id: number

@belongsTo(() => User)
public author: BelongsTo<typeof User>

@column()
public userId: number

Expand All @@ -30,10 +27,7 @@ export default class App extends BaseModel {
public source_url: string

@column()
public image: string

@column()
public releases: string
public path: string

@column()
public category: AppCategory
Expand Down
2 changes: 1 addition & 1 deletion database/migrations/1698679914813_apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class extends BaseSchema {
table.string('desc', 350).nullable().defaultTo("")
table.string('source_url', 300).nullable()
table.string('image', 300).nullable()
table.string('releases', 300).nullable()
table.string('path', 300).nullable()

table.smallint('category').notNullable()
table.bigint("downloads").unsigned().notNullable().defaultTo(0)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"@adonisjs/session": "^6.2.0",
"@adonisjs/shield": "^7.0.0",
"@adonisjs/view": "^6.1.0",
"adm-zip": "^0.5.16",
"axios": "^1.6.2",
"highlight.js": "^11.9.0",
"luxon": "^3.4.0",
Expand Down
Binary file added public/img/fulllogo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 79dda07

Please sign in to comment.