Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 08-13-2009, 11:54 AM
rin rin is offline
 
Join Date: Aug 2004
Location: Mainland of China
Posts: 109
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Attachment.php

Hello,

I was wondering about the following:
If I download or view an attachment via the vBulletin attachment.php, would it be possible to write its filesize into the database?
My idea is this: Every time a member downloads something, the filesize will be added to a user-specific table.
Once the number in that table matches or is bigger than the allowed quota, the attachment.php would return an error. That quota-number in the table would then periodically be erased.
Generally, the question is: Can I get this data (attachment filesize) and write it into the database?
Reply With Quote
  #2  
Old 08-13-2009, 12:06 PM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The filesize for each attachment is already stored in the database. It is stored in the "filesize" field in attachment.
Reply With Quote
  #3  
Old 08-13-2009, 12:23 PM
rin rin is offline
 
Join Date: Aug 2004
Location: Mainland of China
Posts: 109
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Great, I have just installed a test installation and can see what you refer to.
I don't have the chance to check for filesystem attachments, but I assume it is the same method, each attachment getting its own attachment ID and the filesize info in the database.

So in order to control a users bandwidth consumption, I would grab the attachments filesize when attachment.php is called and add it to the user specific attachment_quota table.

Can you point me to a tutorial how to write vBulletin addons? As I tried looking around but didn't find anything.
Or would you suggest that I hard-code it into the files?
Reply With Quote
  #4  
Old 08-15-2009, 01:15 AM
rin rin is offline
 
Join Date: Aug 2004
Location: Mainland of China
Posts: 109
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Gotta bump this. =)
Reply With Quote
  #5  
Old 08-15-2009, 03:51 AM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

There isn't really one particular tutorial on how to write a product. You can look through the articles forum, but I think most users learn by looking at other products to see how it was done.
Reply With Quote
  #6  
Old 08-15-2009, 03:14 PM
AndrewD AndrewD is offline
 
Join Date: Jul 2002
Location: Scotland
Posts: 3,486
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Instead of patching the attachment system, you could try the Links and Downloads Manager, which includes bandwidth control. See url in my signature.
Reply With Quote
  #7  
Old 08-16-2009, 12:40 AM
rin rin is offline
 
Join Date: Aug 2004
Location: Mainland of China
Posts: 109
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by AndrewD View Post
Instead of patching the attachment system, you could try the Links and Downloads Manager, which includes bandwidth control. See url in my signature.
Hello Andrew. I am not sure, about the addon LDM. Currently I am serving more than 120.000 attachments (100+ GB) and how I understand LDM, it will only account for the attachments uploaded after the addon has been activated.
Seeing how many attachments I am serving, I would prefer to have the limit as simple as possible. Only adding a new row to all members, summing and controlling their bandwidth usage that way.

--------------- Added [DATE]1250388287[/DATE] at [TIME]1250388287[/TIME] ---------------

Please bear with me. Since I have asked for an addon like this for several days and so far no one else sees its uses, I might want to try and code it myself with my rather limited knowledge of php and mysql.
So if I want to start, I would like to ask you some questions.
The hook to be used for an addon like this should be attachment_start, right?
Then I want to write the code similar to this:
Code:
GET attachment-filesize -> write filesize to member download quota table

check member-usergroup; if is member of X usergroup = limit quota->X bytes
if member-download-quota > usergroup-limit THEN stop and write attachment quota message.
if member-download-quota <= usergroup limit THEN go on.
Additionally, I would set up a cron job that runs for example, every 24 hours and clears the member-download-quota table so the member is able to download again.

Is it a good approach?

--------------- Added [DATE]1250393636[/DATE] at [TIME]1250393636[/TIME] ---------------

So far, I got this:
Code:
if($vbulletin-userinfo['usergroupid'] = 7) { $download_quota = 15728640; }   // Moderators
if($vbulletin-userinfo[download_quota'] < $download_quota)
{
$new_download_quota = $vbulletin-userinfo['download_quota'] + $attachmentinfo['filesize'];

$db->query_write("
			UPDATE " . TABLE_PREFIX . "user SET
				download_quota = $new_download_quota
			WHERE userid = $vbulletin-userinfo[userid]
			")
}
else
{
print "error";
}
First I create a new table in user called "download_quota".
Then I set a different download quota for all existing usergroups. In this case it's 15 MB in bytes.
Then I go check if the users current download_quota is less than the allowed 15MB.
If that is the case, I will add the filesize of the current attachment to the download_quota field for the specific user.
If it's bigger I return error. =)
Is it that simple?
Reply With Quote
  #8  
Old 08-16-2009, 08:53 AM
AndrewD AndrewD is offline
 
Join Date: Jul 2002
Location: Scotland
Posts: 3,486
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by rin View Post
Hello Andrew. I am not sure, about the addon LDM. Currently I am serving more than 120.000 attachments (100+ GB) and how I understand LDM, it will only account for the attachments uploaded after the addon has been activated.
Seeing how many attachments I am serving, I would prefer to have the limit as simple as possible. Only adding a new row to all members, summing and controlling their bandwidth usage that way.

--------------- Added [DATE]1250388287[/DATE] at [TIME]1250388287[/TIME] ---------------

Please bear with me. Since I have asked for an addon like this for several days and so far no one else sees its uses, I might want to try and code it myself with my rather limited knowledge of php and mysql.
So if I want to start, I would like to ask you some questions.
The hook to be used for an addon like this should be attachment_start, right?
Then I want to write the code similar to this:
Code:
GET attachment-filesize -> write filesize to member download quota table

check member-usergroup; if is member of X usergroup = limit quota->X bytes
if member-download-quota > usergroup-limit THEN stop and write attachment quota message.
if member-download-quota <= usergroup limit THEN go on.
Additionally, I would set up a cron job that runs for example, every 24 hours and clears the member-download-quota table so the member is able to download again.

Is it a good approach?

--------------- Added [DATE]1250393636[/DATE] at [TIME]1250393636[/TIME] ---------------

So far, I got this:
Code:
if($vbulletin-userinfo['usergroupid'] = 7) { $download_quota = 15728640; }   // Moderators
if($vbulletin-userinfo[download_quota'] < $download_quota)
{
$new_download_quota = $vbulletin-userinfo['download_quota'] + $attachmentinfo['filesize'];

$db->query_write("
			UPDATE " . TABLE_PREFIX . "user SET
				download_quota = $new_download_quota
			WHERE userid = $vbulletin-userinfo[userid]
			")
}
else
{
print "error";
}
First I create a new table in user called "download_quota".
Then I set a different download quota for all existing usergroups. In this case it's 15 MB in bytes.
Then I go check if the users current download_quota is less than the allowed 15MB.
If that is the case, I will add the filesize of the current attachment to the download_quota field for the specific user.
If it's bigger I return error. =)
Is it that simple?
Your approach looks broadly ok, but my experience is that you will find complications. For example, users can be members of multiple usergroups - which one defines their quota? What happens if a transfer fails to complete? Are you happy with just knowing each user's total transfer or do you want information on the specific files that were downloaded? etc, etc. It's worth thinking these points through before writing any code, just to be clear in your own mind about the consequences of alternative approaches.

I'm not sure of the need for a separate table for the user group allowances, although this is a straightforward solution. Another approach would be to hold this array as a serialised string in the data cache. This would reduce the required sql traffic.
Reply With Quote
  #9  
Old 08-16-2009, 09:10 AM
rin rin is offline
 
Join Date: Aug 2004
Location: Mainland of China
Posts: 109
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hi Andrew, thanks for your reply.
Since most of the attachments are pictures and less than 200KB in filesize, I wouldn't mind broken transfers. If in the membership plan 10MB bandwidth are mentioned, I would secretly allow members a bandwidth consumption of around 15MB to avoid situations like that.
I would reduce usergroups to the basic ones and don't let members have multiple usergroups.
In my case those would simply be: Banned and Guests (no access anyway), Unsubscribed, Subscribed, Actives and the obvious i.e. Moderators and Administrators.

Because I am thinking about a subscription plan for my website. So I need to make it more valueable for members to pay a sum once in a while. I want to achieve that by giving users bandwith (or not).

I have further worked on it:
Code:
if($vbulletin->userinfo['usergroupid'] = 6) { $limitation = 1; }

$result = $db->query_read("SELECT current_bandwidth FROM " . TABLE_PREFIX . "user WHERE userid = $vbulletin->userinfo[userid]); // Get the current bandwidth usage
$current_quota = $result['current_bandwidth'] + $attachmentinfo['filesize'];  // Calculate the current quota being used including the file which the user wants to download now.

if($current_quota < $limitation)
{
 SHOW ATTACHMENT
}
else
{
 DON'T SHOW ATTACHMENT
}
$db_query_write("UPDATE " . TABLE_PREFIX . "user SET current_bandwidth = $current_quota WHERE userid = $vbulletin->userinfo['userid']");
I am going to try this out on my test-installation now. But I am sure it will be broken in one way or another.
So please, if by looking over the code you find anything which doesn't match up, I hope you can help me.
Cheers

--------------- Added [DATE]1250419067[/DATE] at [TIME]1250419067[/TIME] ---------------

As predicted, it doesn't work.
When opening attachments, it will not write the quota into the database.
I use the "attachment_start" hook.

--------------- Added [DATE]1250431666[/DATE] at [TIME]1250431666[/TIME] ---------------

Strange enough, even if I active the following plugin, all thumbnails and attachments are broken.
Code:
$db->query_write("
			UPDATE " . TABLE_PREFIX . "user SET
				current_bandwidth = 10
			WHERE userid = $vbulletin->userinfo[userid]
		");
I have used attachment_start and attachment_display as hooks.
Reply With Quote
  #10  
Old 08-16-2009, 01:49 PM
AndrewD AndrewD is offline
 
Join Date: Jul 2002
Location: Scotland
Posts: 3,486
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by rin View Post
Hi Andrew, thanks for your reply.
Since most of the attachments are pictures and less than 200KB in filesize, I wouldn't mind broken transfers. If in the membership plan 10MB bandwidth are mentioned, I would secretly allow members a bandwidth consumption of around 15MB to avoid situations like that.
I would reduce usergroups to the basic ones and don't let members have multiple usergroups.
In my case those would simply be: Banned and Guests (no access anyway), Unsubscribed, Subscribed, Actives and the obvious i.e. Moderators and Administrators.

Because I am thinking about a subscription plan for my website. So I need to make it more valueable for members to pay a sum once in a while. I want to achieve that by giving users bandwith (or not).

I have further worked on it:
Code:
if($vbulletin->userinfo['usergroupid'] = 6) { $limitation = 1; }

$result = $db->query_read("SELECT current_bandwidth FROM " . TABLE_PREFIX . "user WHERE userid = $vbulletin->userinfo[userid]); // Get the current bandwidth usage
$current_quota = $result['current_bandwidth'] + $attachmentinfo['filesize'];  // Calculate the current quota being used including the file which the user wants to download now.

if($current_quota < $limitation)
{
 SHOW ATTACHMENT
}
else
{
 DON'T SHOW ATTACHMENT
}
$db_query_write("UPDATE " . TABLE_PREFIX . "user SET current_bandwidth = $current_quota WHERE userid = $vbulletin->userinfo['userid']");
I am going to try this out on my test-installation now. But I am sure it will be broken in one way or another.
So please, if by looking over the code you find anything which doesn't match up, I hope you can help me.
Cheers

--------------- Added [DATE]1250419067[/DATE] at [TIME]1250419067[/TIME] ---------------

As predicted, it doesn't work.
When opening attachments, it will not write the quota into the database.
I use the "attachment_start" hook.

--------------- Added [DATE]1250431666[/DATE] at [TIME]1250431666[/TIME] ---------------

Strange enough, even if I active the following plugin, all thumbnails and attachments are broken.
Code:
$db->query_write("
			UPDATE " . TABLE_PREFIX . "user SET
				current_bandwidth = 10
			WHERE userid = $vbulletin->userinfo[userid]
		");
I have used attachment_start and attachment_display as hooks.
In your last example, and I imagine in your full code, there is a syntax error. The following works correctly:

Code:
$vbulletin->db->query_write("
	UPDATE " . TABLE_PREFIX . "user 
	SET current_bandwidth = 10
	WHERE userid = " . $vbulletin->userinfo[userid]
	);
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:10 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04761 seconds
  • Memory Usage 2,279KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (9)bbcode_code
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete